node.js 通过 package.json 安装“全局”npm 依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14657170/
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
Installing "global" npm dependencies via package.json
提问by uglymunky
I have a few "global" dependencies (jshint, csslint, buster, etc..) that I'd like to have automatically installed and executable via the command line when my package is installed via npm install. Is this possible?
我有一些“全局”依赖项(jshint、csslint、buster 等),当我的包通过npm install. 这可能吗?
Currently, I'm doing the following manually:
目前,我正在手动执行以下操作:
npm install -g <package_name>- from within my project:
npm link <package_name>
npm install -g <package_name>- 从我的项目中:
npm link <package_name>
Update:
Just came across this feature requestfor npm. It seems like the scriptsconfig within package.json is the way to go?
更新:刚刚遇到了 npm 的这个功能请求。似乎scriptspackage.json 中的配置是要走的路?
Update Again: Or, after reading the npm docs, maybe I'm supposed to use a .gyp file? I'm confused.
回答by Jonathan Lonowski
It's not possible to specify dependencies as "global" from a package.json. And, this is by design as Isaac states in that feature requestyou referenced:
不可能将依赖项从package.json. 而且,这是设计使然,正如 Isaac在您引用的功能请求中所述:
Yeah, we're never going to do this.
是的,我们永远不会这样做。
But, "binaries" can still be used when a package is installed locally. They'll be in .../node_modules/.bin/. And, you should be able to queue them up with a preinstallscript.
但是,在本地安装软件包时,仍然可以使用“二进制文件”。他们会在.../node_modules/.bin/。而且,您应该能够使用preinstall脚本将它们排入队列。
Though, if the series of commands is rather lengthy (as "jshint, csslint, buster, etc.." would suggest), you may want to look into using a build tool such as gruntto perform the various tasks:
不过,如果这一系列命令相当冗长(如“ jshint、csslint、buster 等”所建议的那样),您可能需要考虑使用构建工具grunt来执行各种任务:
{
// ...,
"scripts": {
"preinstall": "grunt"
}
}
回答by EndangeredMassa
I really like the pattern where you install local dependencies, then use a bash script that sets your PATH to ./node_modules/.bin.
我真的很喜欢你安装本地依赖项的模式,然后使用一个 bash 脚本将你的 PATH 设置为./node_modules/.bin.
File: env.sh
文件:env.sh
# Add your local node_modules bin to the path for this command
export PATH="./node_modules/.bin:$PATH"
# execute the rest of the command
exec "$@"
Then, you can use this script before any bash command. If you pair that with a Makefile or npm script:
然后,您可以在任何 bash 命令之前使用此脚本。如果将其与 Makefile 或 npm 脚本配对:
File: Makefile
文件:生成文件
lint :
./env.sh csslint my_styles
File: package.json
文件:package.json
"scripts": {
"lint": "./env.sh csslint my_styles"
}
This tasks in these files look like they reference csslint in some global location, but they actually use the version in your node_modules bin.
这些文件中的这些任务看起来像是在某个全局位置引用了 csslint,但实际上它们使用的是 node_modules bin 中的版本。
The really awesome benefit of this is that these dependencies can be versioned easily, just like your other node modules. If you stick with a global install solution, you could be clobbering some specific version on the user's system that is required for one of their other projects.
这样做的真正好处是可以轻松地对这些依赖项进行版本控制,就像您的其他节点模块一样。如果您坚持使用全局安装解决方案,您可能会破坏用户系统上的某个特定版本,而这些版本是他们其他项目之一所需的。
回答by CMCDragonkai
You should try this: https://github.com/lastboy/package-script
你应该试试这个:https: //github.com/lastboy/package-script
I've been using it to install global npm packages straight from the package.json. It works well for clients who aren't technically literate.
我一直在使用它直接从 package.json 安装全局 npm 包。它适用于没有技术知识的客户。
It even checks if the packages are already installed, if not install them!
它甚至检查软件包是否已经安装,如果没有安装它们!

