更改 node_modules 位置 [英] Change node_modules location
问题描述
有没有办法改变 node_modules 文件夹的位置?
Is there a way to change the node_modules folder location?
例如:
- dir1
- dir2
- node_modules
到:
- dir1
- dir2
- node_modules
推荐答案
以下是默认在node_modules
文件夹下的代码
The following is the code which looks int the node_modules
folder by default
Module.prototype.load = function(filename) {
debug('load ' + JSON.stringify(filename) +
' for module ' + JSON.stringify(this.id));
assert(!this.loaded);
this.filename = filename;
this.paths = Module._nodeModulePaths(path.dirname(filename));
var extension = path.extname(filename) || '.js';
if (!Module._extensions[extension]) extension = '.js';
Module._extensions[extension](this, filename);
this.loaded = true;
};
因此,以下是确切的搜索模式:
So, following is the exact search pattern:
Node.JS 查看给定的模块是否是核心模块.(例如,
http
、fs
等)在加载中始终具有优先权模块.
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.
如果给定的模块不是核心模块(例如 http
、fs
等),Node.js 将开始搜索目录命名,node_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
.
它将从当前目录开始(相对于 Node.JS 中当前正在执行的文件),然后在文件夹层次结构中向上工作,检查 node_modules 文件夹的每个级别.一旦 Node.JS 找到 node_modules
文件夹,它就会尝试将给定的模块加载为 (.js)JavaScript 文件或作为命名子目录;如果找到指定的子目录,它将尝试以各种方式加载文件.所以,例如
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 example
如果您请求加载模块utils"及其目录而不是 .js 文件,则:
Node.JS 将通过以下方式搜索 node_modules
和 utils
的分层目录:
If 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
and utils
in the following ways:
./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
如果 Node.JS 在上述步骤中仍然找不到文件,Node.js 将开始查找目录来自环境变量的路径,即 NODE_PATH
在您的机器上设置(如果您在 Windows 上,显然由 Node.JS 安装程序文件设置)在上述所有步骤中未找到,然后将堆栈跟踪打印到 stder
Eg:错误:
找不到模块你的文件"
有关更多信息:链接是这里甚至循环 require() 是解释得很好..
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 stder
E.g.: Error:
Cannot find module 'yourfile'
For more information: link is here even the cyclic require() is explained very well..
这篇关于更改 node_modules 位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!