如何设置本地安装npm软件包的自定义位置? [英] How to set custom location for local installation of npm package?
问题描述
是否可以通过命令标志或环境变量为npm install
指定自定义程序包目的地?
Is it possible to specify a custom package destination for npm install
, either through a command flag or environment variable?
默认情况下,npm本地安装最终位于当前目录的node_modules
中,但我希望将其安装到其他目录(例如vendor/node_modules
)中的node_modules
中.我该如何实现?
By default, npm local installs end up in node_modules
within the current directory, but I want it to install into node_modules
within a different directory, for example vendor/node_modules
. How can I make that happen?
推荐答案
TL; DR
您可以通过使用--prefix
标志和--global
*标志来做到这一点.
TL;DR
You can do this by using the --prefix
flag and the --global
* flag.
pje@friendbear:~/foo $ npm install bower -g --prefix ./vendor/node_modules
bower@0.7.0 /Users/pje/foo/vendor/node_modules/bower
*即使这是全局"安装,除非PATH
中存在~/foo/vendor/node_modules
,否则无法通过命令行访问已安装的垃圾箱.
*Even though this is a "global" installation, installed bins won't be accessible through the command line unless ~/foo/vendor/node_modules
exists in PATH
.
npm
的每个可配置属性都可以在六个不同位置中的任何一个中进行设置.按优先顺序:
Every configurable attribute of npm
can be set in any of six different places. In order of priority:
- 命令行标志:
--prefix ./vendor/node_modules
- 环境变量:
NPM_CONFIG_PREFIX=./vendor/node_modules
- 用户配置文件:
$HOME/.npmrc
或userconfig
参数 - 全局配置文件:
$PREFIX/etc/npmrc
或userconfig
参数 - 内置配置文件:
path/to/npm/itself/npmrc
- 默认配置: node_modules/npmconf/config-defs.js
- Command-Line Flags:
--prefix ./vendor/node_modules
- Environment Variables:
NPM_CONFIG_PREFIX=./vendor/node_modules
- User Config File:
$HOME/.npmrc
oruserconfig
param - Global Config File:
$PREFIX/etc/npmrc
oruserconfig
param - Built-In Config File:
path/to/npm/itself/npmrc
- Default Config: node_modules/npmconf/config-defs.js
默认情况下,本地安装的软件包进入./node_modules
.全局变量进入prefix
配置变量(默认为/usr/local
).
By default, locally-installed packages go into ./node_modules
. global ones go into the prefix
config variable (/usr/local
by default).
您可以运行npm config list
来查看当前配置,并运行npm config edit
来进行更改.
You can run npm config list
to see your current config and npm config edit
to change it.
通常,npm
的文档确实很有帮助. 文件夹部分很好地概述了npm和
In general, npm
's documentation is really helpful. The folders section is a good structural overview of npm and the config section answers this question.
这篇关于如何设置本地安装npm软件包的自定义位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!