nodesto
nodesto

Reputation: 505

Module scope in Node.js

I am having a hard time figuring out how this following module scope works in node.js.

main.js

module.exports = App = {
  add: function(a, b) {
    return a + b;
  }
}

var getNumber = require('./module');
var result = App.add(100, getNumber());

module.js

var number = 200;

module.exports = function () {
  console.log(App); // App is visible here - how come?
  return number;
};

I wonder why App is visible in the module, since it is not required in. If I no longer export App, it is not visible.

Upvotes: 1

Views: 337

Answers (2)

soundyogi
soundyogi

Reputation: 369

App is on the Global Scope:

foo = {}

foo.bar = baz = 5

console.log(baz)

// baz is available on the global scope

Upvotes: 1

John
John

Reputation: 31

Since you didn't declare var App, App became a global variable implicitly. This happens even if you don't have module.exports at all.

Upvotes: 4

Related Questions