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
});
Related Contents:
- Node.js version on the command line? (not the REPL)
- The difference between “require(x)” and “import x”
- Folder structure for a Node.js project
- How to include scripts located inside the node_modules folder?
- npm install from Git in a specific version
- How can I generate an ObjectId with mongoose?
- Why does mongoose always add an s to the end of my collection name
- What do the return values of node.js process.memoryUsage() stand for?
- How to install only “devDependencies” using npm
- Do you need to use path.join in node.js?
- NPM modules won’t install globally without sudo
- npm install error – unable to get local issuer certificate
- E: Unable to locate package npm
- How can I get the path of a module I have loaded via require that is *not* mine (i.e. in some node_module)
- Having error “Module ‘name’ resolves to an untyped module at…” when writing custom TypeScript definition file
- Where does forever store console.log output?
- How can I set an environmental variable in node.js?
- How to install a specific version of Node on Ubuntu?
- How do I parse a HTML page with Node.js
- Using PassportJS, how does one pass additional form fields to the local authentication strategy?
- Why should I use Restify?
- Global Node modules not installing correctly. Command not found
- npm: Why is a version “0.1” invalid?
- Managing Sessions in Node.js? [closed]
- Node.js get image from web and encode with base64
- Difference between Node js and express js [closed]
- Node.js – inheriting from EventEmitter
- tsc is not recognized as internal or external command
- How to set npm credentials using `npm login` without reading from stdin?
- Uploading file using POST request in Node.js
- How can I get a list of callbacks in the Node work queue? (or, Why won’t Node exit?)
- What the scenario call fs.close is necessary
- Could someone explain what “process.argv” means in node.js please?
- Passing environment variables in npm-scripts
- Assigning a domain name to localhost for development environment
- npm ERR! could not determine executable to run
- How do you query for a non-existent (null) attribute in DynamoDB
- How can I increase the maximum call stack size in Node.js
- Node.js “FATAL ERROR: JS Allocation failed – process out of memory” — possible to get a stack trace?
- How to debug a socket hang up error in NodeJS?
- How to “Ping” from a Node.js app?
- Passing arguments to npm script in package.json [duplicate]
- Can I get node –inspect to open Chrome automatically
- How do I use chmod with Node.js
- Node.js – Using the async lib – async.foreach with object
- less-style markdown viewer for UNIX systems
- Getting Error 402 while publishing package using npm
- How to access Google Chrome’s IndexedDB/LevelDB files?
- How I can use “LIKE” operator on mongoose?
- when to disconnect and when to end a pg client or pool