node.js 如何为本地安装 npm 包设置自定义位置?

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

How to set custom location for local installation of npm package?

node.jsnpm

提问by pje

Is it possible to specify a custom package destination for npm install, either through a command flag or environment variable?

是否npm install可以通过命令标志或环境变量为 指定自定义包目标?

By default, npm local installs end up in node_moduleswithin the current directory, but I want it to install into node_moduleswithin a different directory, for example vendor/node_modules. How can I make that happen?

默认情况下,npm local installs 最终在node_modules当前目录中,但我希望它安装到node_modules不同的目录中,例如vendor/node_modules. 我怎样才能做到这一点?

回答by pje

TL;DR

TL; 博士

You can do this by using the --prefixflag and the --global* flag.

您可以通过使用--prefix标志和--global* 标志来做到这一点。

pje@friendbear:~/foo $ npm install bower -g --prefix ./vendor/node_modules
[email protected] /Users/pje/foo/vendor/node_modules/bower

*Even though this is a "global" installation, installed bins won't be accessible through the command line unless ~/foo/vendor/node_modulesexists in PATH.

*即使这是一个“全局”安装,除非~/foo/vendor/node_modules存在于PATH.

TL;R

TL;R

Every configurable attribute of npmcan be set in any of six different places. In order of priority:

的每个可配置属性npm都可以在六个不同位置中的任何一个进行设置。按优先顺序:

  • Command-Line Flags: --prefix ./vendor/node_modules
  • Environment Variables: NPM_CONFIG_PREFIX=./vendor/node_modules
  • User Config File: $HOME/.npmrcor userconfigparam
  • Global Config File: $PREFIX/etc/npmrcor userconfigparam
  • Built-In Config File: path/to/npm/itself/npmrc
  • Default Config: node_modules/npmconf/config-defs.js
  • 命令行标志: --prefix ./vendor/node_modules
  • 环境变量: NPM_CONFIG_PREFIX=./vendor/node_modules
  • 用户配置文件: $HOME/.npmrcuserconfig参数
  • 全局配置文件: $PREFIX/etc/npmrcuserconfig参数
  • 内置配置文件: path/to/npm/itself/npmrc
  • 默认配置: node_modules/npmconf/config-defs.js

By default, locally-installed packages go into ./node_modules. global ones go into the prefixconfig variable (/usr/localby default).

默认情况下,本地安装的包进入./node_modules. 全局prefix变量进入配置变量(/usr/local默认情况下)。

You can run npm config listto see your current config and npm config editto change it.

您可以运行npm config list以查看当前配置并npm config edit进行更改。

PS

聚苯乙烯

In general, npm's documentation is really helpful. The folderssection is a good structural overview of npm and the configsection answers this question.

一般来说,npm的文档真的很有帮助。该文件夹的部分是新公共管理的一个良好的结构概述和配置部分回答了这个问题。

回答by Rimian

If you want this in config, you can set npm config like so:

如果你想在配置中使用它,你可以像这样设置 npm config:

npm config set prefix "$(pwd)/vendor/node_modules"

or

或者

npm config set prefix "$HOME/vendor/node_modules"

Check your config with

检查您的配置

npm config ls -l

npm config ls -l

Or as @pje says and use the --prefixflag

或者正如@pje所说并使用--prefix标志

回答by Ryan

For OSX, you can go to your user's $HOME(probably /Users/yourname/) and, if it doesn't already exist, create an .npmrcfile (a file that npm uses for user configuration), and create a directory for your npm packages to be installed in (e.g., /Users/yourname/npm). In that .npmrc file, set "prefix" to your new npm directory, which will be where "globally" installed npm packages will be installed; these "global" packages will, obviously, be available only to your user account.

对于 OSX,您可以转到您的用户$HOME(可能是 /Users/yourname/),如果它不存在,请创建一个.npmrc文件(npm 用于用户配置的文件),并为您的 npm 包创建一个目录以安装在(例如,/Users/yourname/npm)。在该 .npmrc 文件中,将“前缀”设置为新的 npm 目录,该目录将安装“全局”安装的 npm 包;显然,这些“全局”软件包仅对您的用户帐户可用。

In .npmrc:

在 .npmrc 中:

prefix=${HOME}/npm

prefix=${HOME}/npm

Then run this command from the command line:

然后从命令行运行此命令:

npm config ls -l

npm config ls -l

It should give output on both your own local configuration and the global npm configuration, and you should see your local prefix configuration reflected, probably near the top of the long list of output.

它应该给出你自己的本地配置和全局 npm 配置的输出,你应该看到你的本地前缀配置被反映,可能在输出的长列表的顶部附近。

For security, I recommend this approach to configuring your user account's npm behavior over chown-ing your /usr/localfolders, which I've seen recommended elsewhere.

为了安全起见,我建议使用这种方法来配置您的用户帐户的 npm 行为,而不是 chown-ing 您的/usr/local文件夹,我在其他地方看到过推荐这种方法。

回答by MichaelHuelsen

On Windows 7 for example, the following set of commands/operations could be used.

例如,在 Windows 7 上,可以使用以下命令/操作集。

Create an personal environment variable, double backslashes are mandatory:

创建个人环境变量,双反斜杠是强制性的:

  • Variable name: %NPM_HOME%
  • Variable value: C:\\SomeFolder\\SubFolder\\
  • 变量名: %NPM_HOME%
  • 变量值: C:\\SomeFolder\\SubFolder\\

Now, set the config values to the new folders (examplary file names):

现在,将配置值设置为新文件夹(示例文件名):

  • Set the npm folder
  • 设置 npm 文件夹

npm config set prefix "%NPM_HOME%\\npm"

npm config set prefix "%NPM_HOME%\\npm"

  • Set the npm-cache folder
  • 设置 npm-cache 文件夹

npm config set cache "%NPM_HOME%\\npm-cache"

npm config set cache "%NPM_HOME%\\npm-cache"

  • Set the npm temporary folder
  • 设置 npm 临时文件夹

npm config set tmp "%NPM_HOME%\\temp"

npm config set tmp "%NPM_HOME%\\temp"

Optionally, you can purge the contents of the original folders beforethe config is changed.

或者,您可以更改配置之前清除原始文件夹的内容。

  • Delete the npm-cache npm cache clear

  • List the npm modules npm -g ls

  • Delete the npm modules npm -g rm name_of_package1 name_of_package2

  • 删除 npm-cache npm cache clear

  • 列出 npm 模块 npm -g ls

  • 删除 npm 模块 npm -g rm name_of_package1 name_of_package2

回答by Oli Studholme

After searching for this myself wanting several projects with shared dependencies to be DRYer, I've found:

我自己搜索了几个具有共享依赖项的项目作为 DRYer 之后,我发现:

  • Installing locally is the Node way for anything you want to use via require()
  • Installing globally is for binaries you want in your path, but is not intended for anything via require()
  • Using a prefix means you need to add appropriate binand manpaths to $PATH
  • npm link(info) lets you use a local install as a source for globals
  • 本地安装是 Node 方式,可以通过以下方式使用任何东西 require()
  • 全局安装适用于您在路径中想要的二进制文件,但不适用于任何通过 require()
  • 使用你需要适当添加前缀手段binman路径$PATH
  • npm link( info) 允许您使用本地安装作为全局变量的来源

→ stick to the Node way and install locally

→ 坚持 Node 方式,本地安装

ref:

参考: