Mendhak
Mendhak

Reputation: 8775

What is the difference between require() and new require()?

The core of my question is, what's the difference between

var fs = new require('fs');

and

var fs = require('fs');

Are there any effects or caveats if I were to use new for all modules everywhere?

While using Webstorm, I noticed that I can get intellisense working only if I use new require('fs'). Before I start using it consistently for a better development experience, I wanted to know a bit more about it.

Upvotes: 3

Views: 4634

Answers (3)

apsillers
apsillers

Reputation: 115930

First of all: Do not confuse new require("...") (which invokes require as a constructor) with new (require("...")) (which invokes the return value of the require call as a constructor). You are doing the first one.

require was not intended to be invoked as a constructor with new. That's not Node-idiomatic, and generally weird. You shouldn't do it, since it reduces the readbility of your code. A future reader might easily mistake it for new (require("...")) (i.e., calling new on a constructor that is being returned by require), since normal Node style does not use new with require.

Now, let's talk about actual side effects.

new causes a function to run its [[Construct]] internal method, which takes the following actions:

  • invoke the function with a this set to a newly-created object whose prototype is set to the function's prototype property

  • Return a result:

    • if the function returns an object, return the result of the function
    • if the function returns anything else, return the newly-created this object

The return value new require will be the same as require for all modules whose exports value is non-primitive (which is true of virtually any module; they typically export a plain object or a function, which is also a kind of object). In the rare case that your module does export a primitive, though, then new will deny you access to that value.

The only other possible difference between require(...) and new require(...) is that the new variant is supplied with a different this value. However, require appears to ignore its this value totally. (Note that Module.prototype.require -- a different function from normal require -- does use its this value, but that's only used when you require submodules from a module, using module.require(...).)

Upvotes: 8

Chetan Bhasin
Chetan Bhasin

Reputation: 3571

The 'new' operator in Node.js is used to create a new instance of something. As you might know in Node.js functions themselves are first class objects. You can create new instance of a function by using 'new' operator that will reside inside a variable.

This is used in order to create many functions with the same body but different name so that they have their variables with different values in a program.

Require, on the other hand, is used to import a module that you have created elsewhere or an existing node modules like you might do by importing libs in other languages. Require in this case returns to the assigned variable the module that you are importing in your application.

When you use 'new' with require, it doesn't do anything but imports a new instance of the module that you are importing. This method is not needed, but it will work.

It is suggested to use new only when you want to create several instances of same object and/or want to clear up the older instance by creating newer instance of another object into the same variable.

Upvotes: 1

qwertynl
qwertynl

Reputation: 3933

Think about it.

require returns something.

It is the same as doing:

var foo = function(bar) {
    this.bar = bar;
};

var f = foo('bar');
console.log(f.bar); //ERROR
//vs:
var y = new foo('bar');
console.log(y.bar); // 'bar'

So all your code does is invoke new with the return from require

Upvotes: -1

Related Questions