Node Modules in JavaScript Explained for Beginners
As Node.js applications grow, writing all your code in a single file quickly becomes unmanageable.
To solve this, Node.js uses modules—a system that lets you split your code into smaller, reusable files.
In this tutorial, you’ll learn:
what Node modules are
types of modules in Node.js
how
requireandmodule.exportsworkbuilt-in vs third-party modules
common mistakes and best practices
Let’s start with the basics.
What Are Node Modules?
A Node module is a reusable block of code contained in a file or folder.
Each file in Node.js is treated as a separate module with its own scope.
This means:
variables are not global by default
code is isolated and safe
functionality can be reused
Why Node Modules Matter
Without modules:
code becomes hard to maintain
naming conflicts occur
reuse is difficult
With modules:
better organization
easier debugging
scalable applications
Types of Node Modules
Node.js supports three main types of modules:
Built-in modules
User-defined modules
Third-party modules
Built-in Modules
Node.js ships with several built-in modules.
Some common ones are:
fs(file system)httppathos
Example: Using the fs Module
const fs = require("fs");
fs.writeFileSync("test.txt", "Hello Node");
No installation is required for built-in modules.
User-Defined Modules
You can create your own module by exporting code from a file.
Exporting a Module
// math.js
function add(a, b) {
return a + b;
}
module.exports = add;
Importing a Module
// app.js
const add = require("./math");
console.log(add(2, 3));
✔ Use ./ for local files
✔ File extension is optional in Node
Exporting Multiple Values
Using an Object
// utils.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };
Importing:
const { add, subtract } = require("./utils");
Understanding module.exports vs exports
module.exports
This is the actual object returned by require().
exports
exports is just a reference to module.exports.
Correct usage:
exports.add = add;
Incorrect usage:
exports = add; // breaks the reference
Third-Party Modules (npm Packages)
Node.js uses npm (Node Package Manager) to install external libraries.
Installing a Package
npm install lodash
Using a Third-Party Module
const _ = require("lodash");
console.log(_.chunk([1, 2, 3, 4], 2));
Node looks inside node_modules automatically.
The require() Function Explained
When you use require():
Node checks built-in modules
Then checks
node_modulesThen checks local files
The loaded module is cached, so it runs only once.
Module Scope in Node.js
Each module has its own scope.
const secret = "hidden";
This variable is not accessible outside the file unless exported.
Common Mistakes
❌ Forgetting ./ for local modules
require("math"); // wrong
❌ Overwriting exports incorrectly
exports = function () {}; // wrong
❌ Modifying node_modules manually
Always use npm.
Node Modules vs ES6 Modules
| Feature | Node (CommonJS) | ES6 Modules |
| Syntax | require / module.exports | import / export |
| Default in Node | Yes (older) | Modern Node |
| Browser support | No | Yes |
| Dynamic imports | Yes | Limited |
Best Practices
Keep modules small and focused
Use meaningful filenames
Avoid circular dependencies
Do not pollute module exports
Prefer ES modules for new projects
Mental Model
A Node module is a private box that only shares what you explicitly export.
Conclusion
Node modules are the backbone of every Node.js application.
Once you understand them, you’ll be able to:
structure backend apps cleanly
reuse logic effectively
work confidently with npm packages
Master this concept before moving to frameworks like Express.