CommonJs (Node.js) - Module.exports

1 - About

If you want the commonJs 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.

3 - 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()}`);

4 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap