node.js 更改 node_modules 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18974436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:43:36  来源:igfitidea点击:

Change node_modules location

node.jsnpm

提问by M K

Is there a way to change the node_modules folder location?

有没有办法更改 node_modules 文件夹位置?

For example:

例如:

- dir1
- dir2
- node_modules

to:

到:

- dir1
- dir2
    - node_modules

采纳答案by Amol M Kulkarni

The following is the code which looks int the node_modulesfolder by default

以下是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;
};

So, following is the exact search pattern:

因此,以下是确切的搜索模式:

  1. Node.JSlooks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading modules.

  2. 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.JSfinds the node_modulesfolder, 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

  3. If you make a request to load the module, "utils" and its a directory not a .js file then:
    Node.JSwill search a hierarchical directory for node_modulesand utilsin the following ways:

    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
    
  4. If Node.JSstill 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_PATHset 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 hereeven the cyclic require()is explained very well..

  1. Node.JS查看给定的模块是否是核心模块。(例如httpfs等)在加载模块中始终具有优先权。

  2. 如果给定的模块不是核心模块(例如http,,fs等),Node.js 将开始搜索名为,的目录node_modules

    它将从当前目录开始(相对于Node.JS 中当前正在执行的文件),然后在文件夹层次结构中向上工作,检查 node_modules 文件夹的每个级别。一旦Node.JS找到该node_modules文件夹,它就会尝试将给定的模块加载为 (.js) JavaScript 文件或命名的子目录;如果找到指定的子目录,它将尝试以各种方式加载文件。所以,例如

  3. 如果您的请求加载模块“utils的”,其目录不是一个.js文件,然后:
    node.js的将搜索分层目录node_modules,并utils在以下几个方面:

    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
    
  4. 如果Node.JS在上述步骤中仍然找不到文件,Node.js 将开始查看环境变量中的目录路径,即NODE_PATH在您的机器上设置(如果您在 Windows 上,显然由 Node.JS 安装程序文件设置)在所有然后上述步骤没有找到,将打印一个堆栈跟踪stder
    例如 :有关更多信息:链接在这里,甚至循环 require()也得到了很好的解释..Error:Cannot find module 'yourfile'

回答by mohu

In fact, we can even edit the nodevars.batfile in which the last line is:

事实上,我们甚至可以编辑nodevars.bat最后一行所在的文件:

if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"

if "%CD%\"=="%~dp0" cd /d "%HOMEDRIVE%%HOMEPATH%"

We can specify our own directory instead of looking after the user's home directory:

我们可以指定我们自己的目录而不是照看用户的主目录:

%HOMEDRIVE%%HOMEPATH%

%HOMEDRIVE%%HOMEPATH%

so that node-modules-location will automatically looked after.

这样 node-modules-location 将自动得到照顾。