Skip to main content

Command Palette

Search for a command to run...

Node Modules in JavaScript Explained for Beginners

Published
4 min readView as Markdown

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 require and module.exports work

  • built-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:

  1. Built-in modules

  2. User-defined modules

  3. Third-party modules


Built-in Modules

Node.js ships with several built-in modules.

Some common ones are:

  • fs (file system)

  • http

  • path

  • os


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():

  1. Node checks built-in modules

  2. Then checks node_modules

  3. Then 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

FeatureNode (CommonJS)ES6 Modules
Syntaxrequire / module.exportsimport / export
Default in NodeYes (older)Modern Node
Browser supportNoYes
Dynamic importsYesLimited

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.