更改node

编程入门 行业动态 更新时间:2024-10-27 10:29:42
更改node_modules位置(Change node_modules location)

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

例如:

- dir1 - dir2 - node_modules

至:

- dir1 - dir2 - node_modules

Is there a way to change the node_modules folder location?

For example:

- dir1 - dir2 - node_modules

to:

- dir1 - dir2 - node_modules

最满意答案

以下是默认情况下查看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; };

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

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

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

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 looks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading 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.

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

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

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 stderE.g.: Error:Cannot find module 'yourfile' For more information: link is here even the cyclic require() is explained very well..

更多推荐

本文发布于:2023-08-07 14:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464727.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:node

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!