Learn the key differences between module.exports and exports in Node.js and when to use each one.
One of the key features of Node.js is its support for modules, which are reusable blocks of code that can be shared between different parts of an application. In Node.js, modules are defined using the CommonJS module system, which provides two ways to export module functionality: using module.exports
and exports
. In this article, we'll explore the difference between these two methods and when to use each one.
In Node.js, each module is encapsulated in its own scope, which means that its variables and functions are not accessible outside the module unless they are explicitly exported. module.exports
is a special object that allows you to export functionality from a module. When you assign a value to module.exports
, you're essentially replacing the entire exports object with a new object. For example, suppose we have a module called math.js
that exports a function called add
:
// math.js
function add(a, b) {
return a + b;
}module.exports = add;
In this example, we’re assigning the add
function to module.exports
, which means that when we require
this module in another part of our application, we'll get back the add
function:
// index.js
const add = require('./math.js');console.log(add(2, 3)); // Outputs: 5
exports
is simply a reference to module.exports
. When you assign a value to exports
, you're actually modifying the module.exports
object. For example, let's modify our math.js
module to use exports
instead of module.exports
:
// math.js
exports.add = function(a, b) {
return a + b;
};
In this example, we’re assigning an object with a single property add
to exports
. When we require this module in our application, we can access the add
function as a property of the returned object:
// index.js
const math = require('./math.js');console.log(math.add(2, 3)); // Outputs: 5
So, which method should you use to export functionality from your module? In general, you should use module.exports
when you want to export a single function or object from your module, and exports
when you want to export multiple properties or functions from your module. For example, suppose we have a module called utils.js
that exports several utility functions:
// utils.js
function add(a, b) {
return a + b;
}function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
exports.add = add;
exports.subtract = subtract;
exports.multiply = multiply;
In this example, we’re using exports
to export multiple functions from our module. When we require this module in our application, we can access each of these functions as properties of the returned object:
// index.js
const utils = require('./utils.js');console.log(utils.add(2, 3)); // Outputs: 5
console.log(utils.subtract(5, 2)); // Outputs: 3
console.log(utils.multiply(2, 3)); // Outputs: 6
In this article, we’ve explored the difference between module.exports
and exports
in Node.js. We've seen that module.exports
is used to export a single function or object from a module, while exports
is used to export multiple properties or functions. It's important to understand the difference between these two methods so you can choose the right one for your module. By using the appropriate method, you can ensure that your module exports the correct functionality and is easy to use and maintain.
One additional note is that you should avoid using both module.exports
and exports
in the same module, as this can lead to unexpected behavior. When you assign a value to exports
, you're actually modifying module.exports
, so if you use both methods, you may end up exporting the wrong functionality from your module.
In general, if you’re unsure which method to use, it’s safe to stick with module.exports
. This method allows you to export a single function or object from your module, and it's the most commonly used method in Node.js modules.