Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Node.js modules: multiple requires return the same object?

Tags:

node.js

I have a question related to the node.js documentation on module caching:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles.

What is meant with may?

I want to know if require will always return the same object. So in case I require a module A in app.js and change the exports object within app.js (the one that require returns) and after that require a module B in app.js that itself requires module A, will I always get the modified version of that object, or a new one?

// app.js  var a = require('./a'); a.b = 2; console.log(a.b); //2  var b = require('./b'); console.log(b.b); //2  // a.js  exports.a = 1;  // b.js  module.exports = require('./a'); 
like image 635
Xomby Avatar asked Jan 16 '12 22:01

Xomby


People also ask

When you import a NodeJS module What does the require function return?

1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.

Why do you need separate modules in NodeJS?

Each module in Node. js has its own context, so it cannot interfere with other modules or pollute global scope. Also, each module can be placed in a separate . js file under a separate folder.

What is require return NodeJS?

The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.

What happens when we require a module in NodeJS?

If the module is native, it calls the NativeModule. require() with the filename and returns the result. Otherwise, it creates a new module for the file and saves it to the cache. Then it loads the file contents before returning its exports object.


2 Answers

If both app.js and b.js reside in the same project (and in the same directory) then both of them will receive the same instance of A. From the node.js documentation:

... every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.


The situation is different when a.js, b.js and app.js are in different npm modules. For example:

[APP] --> [A], [B] [B]   --> [A] 

In that case the require('a') in app.js would resolve to a different copy of a.js than require('a') in b.js and therefore return a different instance of A. There is a blog post describing this behavior in more detail.

like image 64
Petr Stodulka Avatar answered Sep 26 '22 09:09

Petr Stodulka


node.js has some kind of caching implemented which blocks node from reading files 1000s of times while executing some huge server-projects.

This cache is listed in the require.cache object. I have to note that this object is read/writeable which gives the ability to delete files from the cache without killing the process.

http://nodejs.org/docs/latest/api/globals.html#require.cache

Ouh, forgot to answer the question. Modifying the exported object does not affect the next module-loading. This would cause much trouble... Require always return a new instance of the object, no reference. Editing the file and deleting the cache does change the exported object

After doing some tests, node.js does cache the module.exports. Modifying require.cache[{module}].exports ends up in a new, modified returned object.

like image 42
moe Avatar answered Sep 26 '22 09:09

moe