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));