CommonJs (Node.js) - Module.exports


If you want the commonJs/Node module to be an instance of some class, assign the desired export object to the module.exports property.

Note that assigning the desired object to exports will simply rebind the local exports variable, which is probably not what is desired.

It has the same functionality that the export statement of Es module

Example and Usage

The module.exports property can be assigned a new value (such as a function or object).

// assigning to exports will not modify module, must use module.exports
module.exports = (width) => {
  return {
    area: () => width ** 2
const square = require('./square.js');
const mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);

Documentation / Reference

Discover More
CommonJs (NodeJs) - Require

The import functionality implementation of commonjs (and then node) is done via the require function require basically: reads a Javascript file (generally a CommonJs module but it may be any javascript...
Javascript - CommonJs

CommonJs is a Module format definition They have defined: * a Module * and packages definition The CommonJS...
Javascript Module - Import (Es Module)

This page is the import statement defined in the es module specification The ES6 import is: a function for dynamic import (performed at runtime time) or a statement for static import (performed...
Module - Export (Es Module)

The export statement is the counterpart of the import statement in the implementation of an ES module. modules.export statement of CommonJs transpilers Export declare the module’s public API. You...
WebPack - webpack.config.js

webpack.config.js is the configuration file of Webpack. See The file is generally located to the root directory of the project. where:

Share this page:
Follow us:
Task Runner