Node.js Modules
A module is a JavaScript file you can reuse. Node.js ships with built-in modules like http and fs, and you can write your own. Splitting code into modules keeps projects organized: each file has one job, so a project made of small files is easier to read and debug than one giant file.
Two keywords do the work. require loads a module into your file. module.exports decides what other files receive when they require that module. Think of a module as a room with a locked door. Everything inside is private until you open the door with module.exports. What you put on module.exports is exactly what require returns.
Newer JavaScript has an alternative style with import and export. It does the same job with slightly different syntax, and you will meet it in frontend work and in newer Node.js code. This lesson teaches require and module.exports first because most existing Node.js projects still use them, and the concepts carry over. Once you understand the ideas, switching styles is easy.
A common beginner mistake: forgetting the dot. require("math") looks for a built-in module or an npm package named math, not your file. require("./math") loads a file called math.js next to your code. The dot means a local file, and it is easy to leave out.
A module of your own
Create a file called math.js. It exports a function that adds two numbers. The module.exports line is the door. Anything you put on it is visible to other files, and everything else stays private inside the module. Exporting only what is needed is good practice, because it keeps the outside world from poking into internals.
// math.js
function add(a, b) {
return a + b; // returns the sum of the two arguments
}
module.exports = { add }; // other files can now require this
Use it
Now load your module from another file. Note the dot in require("./math"). A dot means a local file. Without a dot, Node.js looks in the built-in modules and in npm packages. The require call returns whatever module.exports points to, so you can pull out the add function and call it.
// app.js
const math = require("./math"); // "./" means a local file, not a package
console.log(math.add(2, 3)); // prints 5
// Run it: node app.js
// Output: 5
require vs import
require and module.exports are the CommonJS style, the standard Node.js used from the start. import and export are ES modules, the style JavaScript itself standardized later. In Node.js you can write an ES module by naming your file with a .mjs extension. The features are the same; the syntax differs. Most tutorials and npm packages still show CommonJS, so learning require first is the practical choice.
// CommonJS, the default in Node.js
const math = require("./math");
// ES modules, in a file named math-app.mjs
import { add } from "./math.js";
Syntax
// math.js
function add(a, b) {
return a + b; // returns the sum of the two arguments
}
module.exports = { add }; // open the door: other files can use add now
require Cache Means Singleton
require("./db") is cached — every file gets same instance. Mutating it mutates everywhere.
module.exports = { /* singleton */ }
Good to know
- Every Node.js file is a module.
- require loads a module into your file.
- module.exports controls what other files can use.
- Use "./" in require to load local files.