Nodejs Module Path
1. Built-in Modules
If passed to require the function is the Nodejs built-in module name, does not do the path resolution, directly returns the internal module the export object, for example: require(‘http‘) .
2. Nodejs supports ( / ) or the absolute path beginning with the drive letter ( C: ), and also supports ./ the relative path at the beginning.
3.nodejs also supports the third path module path node_modules, similar to Foo/bar, loaded can make the file can also be a package, mainly used to store the NPM module.
Example: In an absolute path to a module, when the module is /home/user/app.js loaded in the module require(‘foo/bar‘) , Nodejs attempts to use the following path in turn
/home/user/node_modules/foo/bar //Node_modules/home/node_modules/foo/bar //Parent directory under the current directory Node_modules my/ Node_modules/foo/bar //recursively up and down the path, knowing the Node_modules directory under the root directory
4.node_path Environment variables
Nodejs allows you to specify additional module search paths through the NODE_PATH environment variable, and in general, when customizing the configuration, Node_path points to the global installation directory of NPM settings node_modules
Cases:
Node_path=/home/user/lib:/home/lib
Windows uses comma to divide good Linux
When require(‘foo/bar‘) the module is loaded in the way it is used, Nodejs tries the following path in turn.
/home/user/lib/foo/bar/home/lib/foo/bar
Nodejs first entry