MOCKSTACKS
EN
Questions And Answers

More Tutorials









NodeJS How modules are loaded

Global Mode


If you installed Node using the default directory, while in the global mode, NPM installs packages into /usr/local/lib/node_modules. If you type the following in the shell, NPM will search for, download, and install the latest version of the package named sax inside the directory /usr/local/lib/node_modules/express:

$ npm install -g express

Make sure that you have sufficient access rights to the folder. These modules will be available for all node process which will be running in that machine.

In local mode installation. Npm will down load and install modules in the current working folders by creating a new folder called node_modules for example if you are in /home/user/apps/my_app a new folder will be created called node_modules /home/user/apps/my_app/node_modules if its not already exist.

Loading modules


When we refer the module in the code, node first looks up the node_module folder inside the referenced folder in required statement If the module name is not relative and is not a core module, Node will try to find it inside the node_modules folder in the current directory. For instance, if you do the following, Node will try to look for the file
./node_modules/myModule.js:

var myModule = require('myModule.js');

If Node fails to find the file, it will look inside the parent folder called ../node_modules/myModule.js. If it fails again, it will try the parent folder and keep descending until it reaches the root or finds the required module.

You can also omit the .js extension if you like to, in which case node will append the .js extension and will search for the file.

Loading a Folder Module


You can use the path for a folder to load a module like this:

var myModule = require('./myModuleDir');

If you do so, Node will search inside that folder. Node will presume this folder is a package and will try to look for a package definition. That package definition should be a file named package.json. If that folder does not contain a package definition file named package.json, the package entry point will assume the default value of index.js, and Node will look, in this case, for a file under the path ./myModuleDir/index.js.

The last resort if module is not found in any of the folders is the global module installation folder.

Conclusion

In this page (written and validated by ) you learned about NodeJS How modules are loaded . What's Next? If you are interested in completing NodeJS tutorial, your next topic will be learning about: NodeJS Cluster Module.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.