更改node_modules的位置 [英] Change node_modules location

查看:1907
本文介绍了更改node_modules的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以更改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:

  1. Node.JS 可以查看给定的模块是否为核心模块. (例如httpfs等)在加载过程中始终优先 模块.

  1. 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.

如果给定的模块不是核心模块(例如httpfs等),则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文件或a.js文件的形式加载给定模块.命名子目录;如果找到命名的子目录,它将尝试以各种方式加载文件.因此,例如

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_modulesutils的分层目录中进行搜索:

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 : Error: Cannot find module 'yourfile'
    有关更多信息:链接位于此处,即使循环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屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆