Node.js uses the CommonJS module system (require / module.exports) and the newer ES Modules system (import / export). npm is the package manager that installs third-party libraries.
CommonJS (most common in Node.js)
// math.js — exporting
function add(a, b) { return a + b }
module.exports = { add }
// app.js — importing
const { add } = require('./math')
console.log(add(2, 3)) // 5Built-in modules
Node.js ships with core modules you can require without installing anything:
const path = require('path')
const os = require('os')
const fs = require('fs')
console.log(path.join('/users', 'alice', 'docs')) // /users/alice/docs
console.log(os.platform()) // linux / darwin / win32Node.js uses the CommonJS module system (require / module.exports) and the newer ES Modules system (import / export). npm is the package manager that installs third-party libraries.
CommonJS (most common in Node.js)
// math.js — exporting
function add(a, b) { return a + b }
module.exports = { add }
// app.js — importing
const { add } = require('./math')
console.log(add(2, 3)) // 5Built-in modules
Node.js ships with core modules you can require without installing anything:
const path = require('path')
const os = require('os')
const fs = require('fs')
console.log(path.join('/users', 'alice', 'docs')) // /users/alice/docs
console.log(os.platform()) // linux / darwin / win32