New Note:你可能不想或不需要这样做.您可能想做的是,将构建/测试等的这些类型的命令依赖项放在包的devDependencies
部分中.json.Anytime you use something from 101 in package.json your devDependencies commands (in node_modules/.bin) act as if they are in your path.
例如:
npm i --save-dev mocha # Install test runner locally
npm i --save-dev babel # Install current babel locally
然后打包.json:
// devDependencies has mocha and babel now
"scripts": {
"test": "mocha",
"build": "babel -d lib src",
"prepublish": "babel -d lib src"
}
然后在命令提示下,您可以运行:
npm run build # finds babel
npm test # finds mocha
npm publish # will run babel first
New NEW Note: For awhile now we have had npx
, which allows you to run the devDependencies commands without needing to add them to your scripts
section (if you want).
例如:
npx webpack
But如果您想全局安装,可以在软件包的脚本部分添加预安装.json:
"scripts": {
"preinstall": "npm i -g themodule"
}
所以实际上我的npm安装再次执行npm安装..这很奇怪,但似乎有效.
Note:如果使用最常见的npm
设置,而全局 node 包需要安装sudo
,则可能会出现问题.一个选项是更改npm
的配置,这样就没有必要了:
npm config set prefix ~/npm
,将$HOME/npm/bin添加到$PATH,方法是将export PATH=$HOME/npm/bin:$PATH
添加到~/.bashrc
.
另一个可能更好的 Select 是只使用nvm
来管理 node ,这样就不会有这个问题.