In what scope are module variables stored in node.js?

node.js

node.js Problem Overview


When I do this in my node.js module:

var abc = '123';

Where does it go? And by this I mean: in the browser it goes in window.abc (if not executed in a function or otherwise)

If I execute this:

abc = '123';

Then I can find it in global.abc, but that's not how I want it.

node.js Solutions


Solution 1 - node.js

Unlike the browser, where variables are by default assigned to the global space (i.e. window), in Node variables are scoped to the module (the file) unless you explicitly assign them to module.exports.

In fact, when you run node myfile.js or require('somefile.js') the code in your file is wrapped as follow:

(function (exports, require, module, __filename, __dirname) {
     // your code is here
});

Solution 2 - node.js

All the other answers are 100% correct, but I thought I would add an expanded/definitive list of the scopes within a Node.js application in case anybody comes across this via Google while starting off learning Node.js or JavaScript:

Global Scope

Anything declared without the var keyword in any file will be accessible from anywhere running in the same instance of the Node server:

// foo.js
bar = 'baz';


// qux.js
console.log(bar); // prints 'baz'

Note that this is widely considered to be a bad idea, because it makes your app strongly 'coupled'– meaning that you'd have to open foo.js to work out why bar = 'baz' in qux.js

Module Scope

Anything declared with the var keyword at the top level (not inside a function or object, or any other block) of a node.js file is in module scope, and will be accessible from anywhere within the same file, but will not exist anywhere else:

// foo.js
var bar = 'baz';
console.log(bar); // prints 'baz'


// qux.js
console.log(bar); // prints 'undefined'
Function Scope

Anything declared using the var keyword within a function will only be accessible from within that function, and not from anywhere else:

// foo.js
function myFunction() {
  var bar = 'baz';
  console.log(bar); // prints 'baz'
}

function myOtherFunction() {
  console.log(bar); // prints 'undefined'
}



// qux.js
console.log(bar); // prints 'undefined'

JavaScript is function scoped. Unlike other (block scoped) languages, variables declared in a block within a function are accessible from anywhere else in that parent function. For example, this means that if you declare a new variable inside inside a loop, it's accessible outside of that loop as well, as long as you're still inside the parent function:

function myFunction() {
  while (thing === true) {
  var bar = 'baz';
  thing = false;
  }
  console.log(bar); // prints 'baz'
}
Shadowing

If you 'redeclare' an existing variable, e.g. use the var keyword with a variable name that has been used already, then the value associated with that variable name is overwritten within the scope of the new declaration:

var bar = 'foo';
console.log(bar) // prints 'foo'

function myFunction() {
  var bar = 'baz';
  console.log(bar);
}
myFunction(); // prints 'baz'
console.log(bar) // prints 'foo'

Solution 3 - node.js

Node has a module scope, so var abc = '123' in a module will create a variable which is scoped to (and therefore, reachable only for code in) that module.

See also http://nodejs.org/api/globals.html#globals_global

Solution 4 - node.js

Pretty old question, and if anyone is curious about ECMA specs about this question, here is the link

And there is no way for direct access for module variables (except for imported modules):

> Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionskeritView Question on Stackoverflow
Solution 1 - node.jsHector CorreaView Answer on Stackoverflow
Solution 2 - node.jsToadfishView Answer on Stackoverflow
Solution 3 - node.jsrobertklepView Answer on Stackoverflow
Solution 4 - node.jsloadaverageView Answer on Stackoverflow