有没有办法改变node_modules文件夹的位置?
例如:
- dir1
- dir2
- node_modules至:
- dir1
- dir2
- node_modules发布于 2013-09-24 14:35:11
以下是缺省情况下在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;
};因此,以下是确切的搜索模式:
http、fs等)http、fs等),Node.js将开始搜索名为node_modules的目录。它将从当前目录(相对于Node.JS中当前执行的文件)开始,然后在文件夹层次结构中向上移动,检查node_modules文件夹的每一层。一旦Node.JS找到node_modules文件夹,它将尝试将给定模块作为(.js) JavaScript文件或指定的子目录加载;如果找到指定的子目录,它将尝试以各种方式加载该文件。例如,
Node.JS将通过以下方式在分层目录中搜索node_modules和utils:
./node_modules/utils.js./node_modules/utils/index.js ./node_modules/utils/package.json
NODE_PATH (如果您在windows上,则显然是由windows安装程序文件设置的)然后,将堆栈跟踪打印到stder例如:Error:Cannot find module 'yourfile'
有关更多信息: link is require即使循环here ()也解释得很好..
发布于 2016-01-07 04:40:55
实际上,我们甚至可以编辑nodevars.bat文件,其中最后一行是:
if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"
我们可以指定自己的目录,而不是查看用户的主目录:
%HOMEDRIVE%%HOMEPATH%
因此,node-modules-location将被自动处理。
https://stackoverflow.com/questions/18974436
复制相似问题