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.
It goes in the local variable scope. In a module, your code is wrapped in a function. You just don't see it,.
Module scope Before modules, a variable declared outside any function was a global variable. In modules, a variable declared outside any function is hidden and not available to other modules unless it is explicitly exported. Exporting makes a function or object available to other modules.
on windows: c/Program\ Files/nodejs/node_modules/npm/npmrc.
By scope we are talking about the visibility of variables in our code; or in other words which parts of the code can access and modify a variable. I regularly find people declaring variables with var in the middle of code, not knowing how JavaScript will scope these.
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 });
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:
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
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'
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' }
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With