Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js, am I creating a new object when "Require"?

Tags:

So, what I'm not sure is that. if in ModuleA, I have:

var mongoose = require('mongoose'); mongoose.connect(pathA); 

And in ModuleB, I have:

var mongoose = require('mongoose'); mongoose.connect(pathB); 

And in the main program, I have:

var mA = require('./moduleA.js'),  mB = require('./moduleB.js'); 

So, when I run the main program, I guess I will create two mongoose "instances"; one connecting to pathA and one connecting to pathB, is that right?

Also, in Module B, before I connect to pathB, is it connected to pathA or nothing?

Thanks.

like image 445
murvinlai Avatar asked Apr 26 '11 18:04

murvinlai


People also ask

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.

How does require work in NodeJS?

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.

What is the difference between import and require in NodeJS?

Require is Non-lexical, it stays where they have put the file. Import is lexical, it gets sorted to the top of the file. It can be called at any time and place in the program. It can't be called conditionally, it always run in the beginning of the file.

What does the require function return?

The value returned by the require function in module y is equal to the module.


1 Answers

I just did a couple of tests with the latest node V0.4.6. I confirmed the following:

  1. The variable returned from "require" is a singleton.
  2. Subsequent changes will change the data of the required module among all other modules that include it.
  3. Mongoose's connection is a bit weird. Even if you disconnect and set it to a new connection path, it still uses the old connection path.

So, what I mean by the above points 1 and 2 is:

If you have a Module Master:

var myStr = 'ABC'; module.exports.appendStr = function(data) {     myStr += ' ' + data;     }; module.exports.output = function() {     console.log("Output: " + myStr); }; 

And if you have two other modules:

Module A

var mc = require('./moduleMaster.js'); var ma = function() {mc.appendStr(' MA '); }; ma.prototype.output = function() {   mc.output(); } module.exports.create = function() {     return new ma(); };  module.exports._class = ma; 

Module B

var mc = require('./moduleMaster.js'); var mb = function() {mc.appendStr(' MB '); }; ma.prototype.output = function() {   mc.output(); } module.exports.create = function() {     return new mb(); };  module.exports._class = mb; 

Now when you run a test script that requires both Module A and Module B, instantiate them and output:

mTestA.output(); mTestB.output(); 

You will get the following output:

ABC MA ABC MA MB 

instead of

ABC MA ABC MB 

Therefore, it is a singleton. not just local to the module.

like image 195
murvinlai Avatar answered Oct 06 '22 11:10

murvinlai