Is there a way to change the node_modules folder location?
For example:
- dir1
- dir2
- node_modules
to:
- dir1
- dir2
- node_modules
Is there a way to change the node_modules folder location?
For example:
- dir1
- dir2
- node_modules
to:
- dir1
- dir2
- node_modules
In fact, we can even edit the
nodevars.bat
file in which the last line is:if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"
We can specify our own directory instead of looking after the user's home directory:
%HOMEDRIVE%%HOMEPATH%
so that node-modules-location will automatically looked after.
The following is the code which looks int the
node_modules
folder by defaultSo, following is the exact search pattern:
Node.JS looks to see if the given module is a core module. (e.g.
http
,fs
, etc.) Always takes the precedence in the loading modules.If the given module is not a core modules(e.g.
http
,fs
, etc.), Node.js will then begin to search for a directory named,node_modules
.It will start in the current directory (relative to the currently-executing file in Node.JS) and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the
node_modules
folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for exampleIf you make a request to load the module, "utils" and its a directory not a .js file then:
Node.JS will search a hierarchical directory for
node_modules
andutils
in the following ways:If Node.JS still can't find the file in above steps, Node.js will then start to look into the directory paths from environment variables i.e.
NODE_PATH
set on your machine(obviously set by Node.JS installer file if you are on windows) Not Found in all the above steps then, prints a stack trace to stderE.g.:
Error:
Cannot find module 'yourfile'
For more information: link is here even the cyclic require() is explained very well..