How does module caching work on NodeJS

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Discover how module caching works in Node.js with `require()`, its benefits, and how to manage it manually to optimize your applications.

Article published on 07/02/2025, last updated on 10/08/2026

Node.js has a module caching mechanism that improves application performance by avoiding reloading and re-executing the code of already imported modules.

Let's look at this mechanism together, in detail.

How does module caching work?

When a module is imported using require(), Node reads and executes the corresponding file, then caches its export.

If the same module is imported again in another file or in the same file, Node.js does not re-execute its code but directly returns the cached version.

Let's take a simple example to illustrate this mechanism.

Let's create a hello.js file containing a display function and a console.log() outside of this function:

console.log("Module chargé");

function hello(name) {
    console.log("Hello " + name);
}

module.exports = hello;

Now let's import this module several times in another file:

const hello1 = require('./hello.js');
const hello2 = require('./hello.js');
const hello3 = require('./hello.js');

hello1("one");
hello2("two");
hello3("three");

And here's what is displayed in the console:

Module chargé
Hello one
Hello two
Hello three

We can see here that the line console.log("Module chargé") is executed only once, whereas the hello() function is indeed called on each invocation.

Why this caching?

This mechanism offers several advantages:

  1. Improved performance: avoids unnecessarily re-reading and re-executing the same file.
  2. Memory savings: reduces the duplication of objects in memory.
  3. State sharing: modules exporting objects or class instances retain their state across all imports.

Manual cache management

How does the cache work?

Node.js stores loaded modules in a global object called require.cache

This object contains references to the imported modules, allowing them to be reused without having to reload them. You can examine the cache of loaded modules by displaying require.cache:

console.log(require.cache);
{
  '/Users/username/module/hello.js': //key
      Module {
          id: '/Users/username/module/hello.js',
          exports: [Function: hello],
          parent: { /* ... */},
          filename: '/Users/username/module/hello.js',
          loaded: true,
          children: [],
          paths: [/* ... */]
      }
}

Each key in this object corresponds to the absolute path of the loaded module.

And its value is an object representing the module's metadata.

How do you remove a module from the cache?

In some cases, it may be necessary to force a module to reload, for example when making dynamic changes.

This can be done by removing the entry from the cache, like this:

delete require.cache[require.resolve('./hello.js')];
const helloReloaded = require('./hello.js');

This approach should be used with caution, as it can introduce inconsistencies in the application.


Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet