There are two popular ways of exporting modules in JavaScript:
require
and module.exports
to import and export modules. Node.js heavily relies on this format.import
keyword for importing modules and export
keyword for exporting modules. You will find this way of importing and exporting modules in front-end frameworks such as React, Angular, and Vue.In this tutorial, you will learn the CommonJS way of exporting modules in Node.js. If you are interested in learning how to export modules in JavaScript, then visit Export in JavaScript.
Named exports and default exports are two ways that enable you to export functions, classes, constants, variables, objects, arrays and others. Let's begin the explanation with named exports.
Note: To learn about default exports, visit What is export default in Node.js?
Named exports allow you to export anything from a module with another name. To understand it in a better way, let's create a module that exports function, variable, object, class and array defined inside the module. The filename is template.js
.
//template.js file function add(a, b){ return a+b; } var msg = 'Hello, World!'; let arr = [21, 5, 69]; class Rectangle{ constructor(length, width){ this.length = length; this.width = width; } area(){ return this.length*this.width; } } let person = { firstName: 'Mohit', lastName: 'Natani' } module.exports.sum = add; module.exports.Rectangle = Rectangle; module.exports.msg = msg; module.exports.ar = arr; module.exports.person = person; console.log(module);
In the above code, add()
function is exported as sum
, array arr
is exported as ar
, Rectangle
class is exported as Rectangle
, msg
variable is exported as msg
, and person
object is exported as person
.
In this way, you can change the name of exported function, array, class and other variables. In addition to this, you can also write named exports in the following way:
module.exports = {sum: add, Rectangle, msg, ar: arr, person}
Both ways of using named exports work in the same way. You can use anything that you like. If you console.log
module
object in template.js
file, you will find 'exports
' object which contains the information about the module's exported material.
Module { id: '.', path: '/home/runner/DroopyDefiantIntegers', exports: { sum: [Function: add], ar: [ 21, 5, 69 ], Rectangle: [class Rectangle], msg: 'Hello, World!', person: { firstName: 'Mohit', lastName: 'Natani' } }, parent: null, filename: '/home/runner/DroopyDefiantIntegers/template.js', loaded: false, children: [], paths: [ '/home/runner/DroopyDefiantIntegers/node_modules', '/home/runner/node_modules', '/home/node_modules', '/node_modules' ] }
You can see that 'exports
' key has five properties: sum
, ar
, Rectangle
, msg
, and person
. These are the five properties that you can access after importing this module.
Note: Instead of module.exports
, you can use exports
in your code. Both work in the same. You can use any of them in your code.
Next, you will learn the two different ways of importing module.
//app.js file const anyName = require('./template'); let rect = new anyName.Rectangle(10, 20); console.log(`Area of rectangle: ${rect.area()}`); console.log(`Array: ${anyName.ar}`); console.log(`Variable: ${anyName.msg}`); console.log(`Sum of 2 and 3 is ${anyName.sum(2, 3)}`); console.log(`Full Name: ${anyName.person.firstName} ${anyName.person.lastName}`);
Note: You can use template.js
instead of template
. But since the filename has .js
extension, there is no need to add .js
at the end of the module name.
In this code, you imported the entire module and called exported functions, classes, variables and others using dot notation.
Output
Area of rectangle: 200 Array: 21,5,69 Variable: Hello, World! Sum of 2 and 3 is 5 Full Name: Mohit Natani
Instead of importing the entire module, you can use object destructuring to import a specific function, class, variable, array, object and others. Let's import function, array, class, and object from template.js
. As you know that the template.js
has exported add()
function as sum
. So, you have to use sum
, not add
. Similarly, array arr
is exported as ar
, so you have to import it as ar
, not arr
. Rest, you can import Rectangle
class and person
object as it is.
//app.js file const {sum, person, ar, Rectangle} = require('./template') let rect = new Rectangle(10, 20); console.log(`Area of rectangle: ${rect.area()}`); console.log(`Array: ${ar}`); console.log(`Sum of 2 and 3 is ${sum(2, 3)}`); console.log(`Full Name: ${person.firstName} ${person.lastName}`);
Output
Area of rectangle: 200 Array: 21,5,69 Sum of 2 and 3 is 5 Full Name: Mohit Natani
As msg
variable is not imported, so you cannot use it in your code.
While importing, you can even change the name of the imported function, class, object, etc. Suppose you want to change the name of Rectangle
class to Rect
and person
object to employee
, use the below method.
//app.js file const {sum, person: employee, ar, Rectangle: Rect} = require('./template') let rect = new Rect(10, 20); console.log(`Area of rectangle: ${rect.area()}`); console.log(`Array: ${ar}`); console.log(`Sum of 2 and 3 is ${sum(2, 3)}`); console.log(`Full Name: ${employee.firstName} ${employee.lastName}`);
In this way, you can change the name of any exported values. It does not modify the module
object of the imported module.