有没有办法更改node_modules文件夹位置?
例如:
- dir1
- dir2
- node_modules
Run Code Online (Sandbox Code Playgroud)
至:
- dir1
- dir2
- node_modules
Run Code Online (Sandbox Code Playgroud)
Amo*_*rni 50
以下是node_modules
默认情况下查找文件夹的代码
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;
};
Run Code Online (Sandbox Code Playgroud)
因此,以下是确切的搜索模式:
Node.JS查看给定模块是否是核心模块.(例如http
,fs
等等)始终优先考虑加载模块.
如果给定模块不是核心模块(例如http
,fs
等等),则Node.js将开始搜索名为的目录node_modules
.
它将在当前目录(相对于Node.JS中当前正在执行的文件)中启动,然后在文件夹层次结构中向上运行,检查node_modules文件夹的每个级别.一旦Node.JS找到该node_modules
文件夹,它就会尝试将给定模块加载为(.js)JavaScript文件或命名子目录; 如果它找到了命名的子目录,它将尝试以各种方式加载该文件.所以,例如
如果您的请求加载模块"utils的",其目录不是.js文件然后:
node.js的将搜索分层目录node_modules
,并utils
在以下几个方面:
./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
Run Code Online (Sandbox Code Playgroud)如果Node.JS仍然无法在上面的步骤中找到该文件,那么Node.js将开始从NODE_PATH
您的机器上设置的环境变量中查看目录路径(如果您在Windows上,显然由Node.JS安装程序文件设置)在所有然后上述步骤没有找到,将打印一个堆栈跟踪stder
例如 :有关更多信息:链接在这里甚至循环require()解释得很好..Error:
Cannot find module 'yourfile'
归档时间: |
|
查看次数: |
52760 次 |
最近记录: |