node.js 安装节点包时可以使用自定义目录名称代替“node_modules”吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21818701/
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 16:51:54  来源:igfitidea点击:

Can a custom directory name be used instead of 'node_modules' when installing node packages?

node.jsnpmconfig

提问by Zia Ur Rehman

I create a package.json, I run npm install, it works alright. It creates a node_modulesdirectory in my root folder (which I can change by using --prefixoption). However, I don't like underscores all that much. I want to change the name of the directory NPM downloads the modules to. I want it to be named nmodsor node-modulesor something like that.

我创建了一个package.json,我运行了npm install,它工作正常。它node_modules在我的根文件夹中创建一个目录(我可以使用--prefix选项更改)。但是,我不太喜欢下划线。我想更改 NPM 将模块下载到的目录的名称。我希望它被命名nmodsnode-modules或类似的东西。

Bower can do a similar thing by reading the directoryproperty inside a .bowerrcfile in the current dir. Is there a way to do the same with NPM?

Bower 可以通过读取当前目录中文件中的directory属性来做类似的事情.bowerrc。有没有办法对 NPM 做同样的事情?

采纳答案by loganfsmyth

There is no way to change it. The node_modulesfolder is actually not specific to NPM, it is part of Node's core module loading system. Seen here in module.js.

没有办法改变它。该node_modules文件夹实际上并不是 NPM 特有的,它是 Node 核心模块加载系统的一部分。在这里看到module.js

Changing it globally as you've mentioned would also potentially break some of the modules you are using too, as modules are sometimes packages with their dependencies already present in node_modulesand changing it would cause that to break.

正如您所提到的那样全局更改它也可能会破坏您正在使用的某些模块,因为模块有时是已经存在其依赖项的包,node_modules并且更改它会导致其中断。

回答by Dirk R

Yarn you can easily achieve this by adding a file called '.yarnrc' with contents like this:

Yarn 您可以通过添加一个名为“.yarnrc”的文件来轻松实现这一点,其内容如下:

# ./.yarnrc
--modules-folder lib

Next time you run 'yarn' it will create the libfolder and install packages into there instead of into node_modules.

下次运行 'yarn' 时,它会创建lib文件夹并将软件包安装到那里,而不是安装到node_modules 中

Now if only we could get 'npm install' to be as clever.

现在,如果我们能让“npm install”变得同样聪明就好了。

回答by MPazik

There is no way to change it in npm, however, there is an option to configure it in yarn package manager.

无法在 npm 中更改它,但是,有一个选项可以在yarn package manager 中对其进行配置。

yarn install --modules-folder <path>