This touches both the module system and how Node’s runtime process loads code. Let’s break it down 👇
📦 What is require
?
-
require
is a built-in function in Node.js used to import modules. -
A module can be:
-
A built-in Node module (e.g.,
fs
,http
) -
A third-party module (from
node_modules
, installed via npm/yarn) -
A local file module (
./myFile.js
)
-
Example:
const fs = require("fs"); // built-in
const express = require("express"); // npm package
const myUtils = require("./utils"); // local file
⚙️ How does require
work in the process?
When you call require(...)
, Node.js goes through these steps internally:
-
Resolve
-
Node figures out what file or module you’re asking for.
-
Checks in order:
-
Is it a core (built-in) module like
fs
? -
Is it in
node_modules
? -
Is it a relative/absolute path?
-
-
-
Load
-
Reads the file (if it’s a
.js
,.json
, or.node
file). -
If
.json
, it parses it. -
If
.js
, it runs the file inside Node’s module wrapper (see below).
-
-
Wrap
-
Node wraps each file in a function for encapsulation:
(function(exports, require, module, __filename, __dirname) { // your code here });
-
That’s why every file has access to
require
,module
,exports
,__dirname
, and__filename
without you declaring them.
-
-
Execute
- Runs the module code in the current process.
-
Cache
-
The result (
module.exports
) is stored inrequire.cache
. -
If you
require
the same file again, Node won’t reload it — it just returns the cached version (for efficiency).
-
🧠 So, in terms of the Node process:
-
require
is not part of JavaScript itself — it’s part of Node’s module loader system. -
It’s tied into the current process:
-
Resolves modules relative to the process’s
cwd()
or the requiring file. -
Loads and executes code inside the process.
-
Caches modules so the process doesn’t re-execute them unnecessarily.
-
✅ In short:
require
is the function that the Node process uses to resolve, load, and cache modules. It runs your files inside a special wrapper, which is why globals like __dirname
, module
, and exports
exist.