Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
Node.js

Node.js Modules

Split code into reusable modules with module.exports and require (CommonJS).

Node.js Modules — 9/55 in the Node.js series. Full playlist companion: Node.js complete course on YouTube. Prefer one article? Read the complete Node.js tutorial.

Short description

Modules keep projects maintainable. Export functions/objects from one file and import them in another with `require` (CommonJS) or `import` (ESM).

math.js + app.js

// math.js
function sum(a, b) {
  return a + b;
}
module.exports = { sum };

// app.js
const { sum } = require('./math');
console.log(sum(2, 3));

Leave a reply

Your email address will not be published. Required fields are marked *