Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the empty parentheses mean after 'require' declaration in Node.js?

In Node.js I see sometimes a declaration like this:

var App = require('express')();

What do the empty brackets '()' at the end mean?

I am suspecting the declaration above is equivalent to something like:

var Express = require('express');
var App = Express();

Is that right?

like image 844
eYe Avatar asked Dec 12 '25 08:12

eYe


1 Answers

As James already answered the module returns a function which is than invoked in this manner.

Here a simple code sample to make it easier understandable.

function a() {
    function b() {
        alert('Alert me!');
    }
    return b;
}
a()();
//alerts 'Alert me!'  
like image 52
Ales Maticic Avatar answered Dec 15 '25 03:12

Ales Maticic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!