如何为本地安装 npm 包设置自定义位置? [英] How to set custom location for local installation of npm package?

查看:61
本文介绍了如何为本地安装 npm 包设置自定义位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

默认情况下,npm local installs 在当前目录中的 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

*即使这是一个全球"安装时,除非 ~/foo/vendor/node_modules 存在于 PATH 中,否则无法通过命令行访问已安装的 bin.

*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/.npmrcuserconfig param
  • 全局配置文件:$PREFIX/etc/npmrcuserconfig param
  • 内置配置文件: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 or userconfig param
  • Global Config File: $PREFIX/etc/npmrc or userconfig 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 和 config 部分回答了这个问题.

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屋!

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