Node.js Module Caching Explained: How require Caches Modules
Node.js caches modules after their first require. Here is how it works and why it matters.
Node.js Module Caching Explained: How require Caches Modules
Node.js caches modules after their first require. Here is how it works and why it matters.
How Caching Works
When you require a file, Node.js runs it once and stores its module.exports in a cache. Subsequent requires of the same file return the cached object without running the file again.
Why It Matters
Caching means a module's code runs once. If you set a variable in a module, every file that requires it sees the same value. This is useful for shared state like database connections and configuration.
Caching Enables Singletons
Because of caching, a module that exports a single object acts as a singleton. Every require returns the same instance. This is how shared database connections and config work in Node.js.
When Caching Surprises You
If you expect a fresh object on each require, caching gives you the same one. This causes bugs when you want independent state. Export a factory function that creates fresh state instead of a shared object.
Cache Keys Are Resolved Paths
Caching is keyed by the resolved file path, not the require string. require('./utils') and require('../folder/utils') resolve to the same file and share the cache entry.
Clearing the Cache
You can clear the cache with delete require.cache[require.resolve('./file')], but this is rarely needed in production. It is useful in testing and development for forcing a module to reload.
The Takeaway
Node.js caches modules after first require, so the same object is returned each time. This enables singletons for shared state but surprises those expecting fresh instances. Use factory functions for fresh state.
When you require a file, Node.js runs it once and stores its module.exports in a cache. Subsequent requires of the same file return the cached object without running the file again, so a module's code runs once.
Because it means a module's code runs once, and every file that requires it sees the same value. This is useful for shared state like database connections and configuration, and enables the singleton pattern without extra code.
Yes. A module that exports a single object acts as a singleton, since every require returns the same cached instance. This is how shared database connections and configuration work in Node.js apps.
Export a factory function that creates fresh state, instead of a shared object. Calling the factory on each use gives you independent instances, while the module itself stays cached.
Use delete require.cache[require.resolve('./file')] to remove the cached module, forcing it to reload on the next require. This is rarely needed in production but useful in testing and development.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

