Module.exports vs exports: Detailed differences between them

In the programming sequence, modules are self-contained units of functionality that can be shared and reused across projects. They make our lives as developers easier, as we can use them to augment our applications with functionality that we haven’t had to write ourselves. Module.exports is of no exception. In addition, in this article, we also show you the main differences between module.exports vs exports. So, whatever you assign to this component will be exposed as a module.

module.exports using in node.js and the difference with exports
module.exports using in node.js and the difference of module.exports vs exports

It is clearly saying that Module.exports can help us to organize and decouple our code for returning purpose, leading to applications that are easier to understand, debug, and maintain. Yet, one question arises: how exactly are you supposed to use this component to build your Node.js programs? So now, we – ArrowHiTech would like to introduce to you guys the steps on how to use it in Node.js and the main difference of module.exports vs exports.

But first, let’s go to the definition.

What is module.exports?

To be honest, you should consider module.exports as the variable that gets returned from require(). It is an empty object by default, and it is fine to change to anything. In other words, this component is an object that the current module returns when it is “required” in another program or module. The following example exposes a simple string message as a module in Message.js.

module.exports = 'Hello world';

So, you’ve just past the definition. Now we would go further in detail.

How to use module.exports in Node.js?

#1. Export objects

The module.exports are an object. So, you can attach properties or methods to it. In the example, we have attached a property SimpleMessage to the exports object. Also, the following example exposes an object with a string property in the Message.js file. This should go like this:

exports.SimpleMessage = 'Hello world';

//or

module.exports.SimpleMessage = 'Hello world';

Now, import and use this module. The require() function will return an object { SimpleMessage : ‘Hello World’} and assign it to the msg variable. So, now you can use msg.SimpleMessage. After that, run the above example by writing node app.js in the command prompt and see the output as shown below.

C:\> node app.js
Hello World

#2. Export function using module.exports

You can attach an anonymous function to module.exports object as shown below.

module.exports = function (msg) { 
    console.log(msg);
};

The msg variable becomes a function expression in the above example. So, you can invoke the function using parenthesis (). After the coding, run the above example and see the output as shown below.

C:\> node app.js
Hello World

#3. Load module from the separate folder

In order to fully optimize this step, you should use the full path of a module file where you have exported it using module.exports. For example, if the log module in the log.js is stored under the utility folder under the root folder of your application, then import it, as shown below.

var log = require('./utility/log.js');

In the above example, . is for the root folder. It specifies the exact path of your module file. Node.js also allows us to specify the path to the folder without specifying the file name. This is because Node assumes that the folder is a package and will try to look for a package definition. The package.json file should be in a module directory. Then, Node.js will find the log.js file using the main entry in package.json and import it.

One thing to remember: If the package.json file does not exist, then it will look for index.js file as a module file by default.

The brief difference between module.exports vs exports

module.exports vs exports
Module.exports vs exports

Module.exports: Some notes to remember – module.exports vs exports

  • It is the object reference that gets returned from the require() calls.
  • The module is automatically created by Node.js.
  • It is just a reference to a plain JavaScript object.
  • Also, it is empty by default

Moreover, there are two ways we can use module.exports:

  • Attaching public methods to it (like we did in the calculator example).
  • Replacing it with our custom object or function.

Exports: Some notes to remember

  • “exports” is just a convenience variable so module authors can write less code
  • Working with its properties is safe and recommended.

(eg.: exports.add = function…)

  • Exports are NOT returned by require() (module.exports is!)

One thing to remember when mentioning to the difference of module.exports vs exports: we can be able to replace the module one with the custom functions or objects. If we do that but still would like to keep using the “exports” shorthand; then “exports” must be re-pointed to the new custom object.

And that’s how it’s done.

Final words of module.exports vs exports

Above all, this blog delivers you a lot of useful information of the breakdown structure on how to use module.exports and the difference between module.exports vs exports. All in all, the use of this module allows us to export values, objects, and styles from Node.js modules. Coupled with the use of require to import other modules, we have a complete ecosystem for composing large programs out of smaller parts. This is crucial when it comes to Node.js development. What’s more, in case you want to dig down further more information or have any questions about this topic, don’t hesitate to CONTACT US.

Tags

Share