What is export default in Node.js?

Default exports enable developers to export only a single class, function, variable, object, array, constant, etc. You write module.exports and assign a component that you want to export as default exports.

Note: If you are interested in learning what is named exports in Node.js, then visit Node.js export modules.

//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 = Rectangle;
console.log(module);

In the code, the Rectangle class is exported as default exports. You can even see this by printing the module object.

Module {
  id: '.',
  path: '/home/runner/DroopyDefiantIntegers',
  exports: [class Rectangle],
  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's value is a Rectangle class. The benefit of using default exports is that you can import the exported component with any name.

//app.js file
const anyName = require('./template');
let rect = new anyName(10, 20);
console.log(`Area of rectangle: ${rect.area()}`);

Output

Area of rectangle: 200

Here, the Rectangle class is imported with the name anyName.

It is recommended that you use default exports only once in the module. If you use it for more than once, then the last executed default exports statement will be considered the final default exports. Let me explain you with the help of an example.

//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 = Rectangle;
module.exports = add;
console.log(module);

In the above code, Rectangle class and add function are exported as default exports. When you console.log module object, you will find that only add function is exported as the default exports because it is the last statement executed for default exports.

Module {
  id: '.',
  path: '/home/runner/DroopyDefiantIntegers',
  exports: [Function: add],
  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'
  ]
}
//app.js file
const anyName = require('./template');
console.log(`Sum of 2 and 3 is ${anyname(2, 3)}`);

Output

Sum of 2 and 3 is 5

How to use named exports and default exports at the same time in Node.js?

When you use named and default exports together, make sure that you use default exports first and then use named exports; otherwise, you will get a weird result. Also, it will be better to log the module object of the module that you want to import. By doing this, you will get a better picture of the exported components.

//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 = Rectangle;
module.exports.ar = arr;
module.exports.person = person;
console.log(module);

Here, Rectangle class is exported as the default export. Array arr is exported as ar and person object is exported as person.

There are two ways of importing named and default exports together in Node.js:

//app.js file
const anyName = require('./template');
const {person, ar} = require('./template');
let rect = new anyName(10, 20);
console.log(`Area of rectangle: ${rect.area()}`);
console.log(`Array: ${ar}`);
console.log(`Full Name: ${person.firstName} ${person.lastName}`);

In this method, you have imported named and default exports separately. Rectangle class is imported as default import, whereas person object and array ar are imported as named imports.

Output

Area of rectangle: 200
Array: 21,5,69
Full Name: Mohit Natani

If you want to import in a single statement, then follow the below code:

//app.js file
const anyName = require('./template');
let rect = new anyName(10, 20);
console.log(`Area of rectangle: ${rect.area()}`);
console.log(`Array: ${anyName.ar}`);
console.log(`Full Name: ${anyName.person.firstName} ${anyName.person.lastName}`);

Here, you have imported the entire module and access exported components using dot notation. Also, default exported component is accessed using anyName.

Recommended Posts