为什么我们需要在全局和本地安装 gulp?
2 个关于 gulp 的手册说我需要先全局安装 gulp(使用 -g 标志),然后再安装一个当地时间.为什么我需要这个?
2 manuals about gulp say that I need to install gulp first globally (with -g flag) and then one more time locally. Why do I need this?
推荐答案
在全局安装工具时,用户可以在任何地方将其用作命令行实用程序,包括节点项目之外.节点项目的全局安装不好,因为它们使部署更加困难.
When installing a tool globally it's to be used by a user as a command line utility anywhere, including outside of node projects. Global installs for a node project are bad because they make deployment more difficult.
npx
与 npm
5.2
捆绑在一起的实用程序解决了这个问题.使用它,您可以调用本地安装的实用程序,如全局安装的实用程序(但您必须以 npx
开头命令).例如,如果你想调用一个本地安装的 eslint
,你可以这样做:
The npx
utility bundled with npm
5.2
solves this problem. With it you can invoke locally installed utilities like globally installed utilities (but you must begin the command with npx
). For example, if you want to invoke a locally installed eslint
, you can do:
npx eslint .
npm 5.2
当在 package.json 的 script
字段中使用时,npm
会在 node_modules
中搜索工具以及全局安装的模块,因此本地安装就足够了.
npm < 5.2
When used in a script
field of your package.json, npm
searches node_modules
for the tool as well as globally installed modules, so the local install is sufficient.
所以,如果您对(在您的 package.json 中)感到满意:
So, if you are happy with (in your package.json):
"devDependencies": {
"gulp": "3.5.2"
}
"scripts": {
"test": "gulp test"
}
等等.并使用 npm run test
运行,那么您根本不需要全局安装.
etc. and running with npm run test
then you shouldn't need the global install at all.
这两种方法对于让人们设置您的项目很有用,因为不需要 sudo
.这也意味着 gulp
将在 package.json 中的版本发生碰撞时更新,因此每个人在使用您的项目开发时都将使用相同版本的 gulp.
Both methods are useful for getting people set up with your project since sudo
isn't needed. It also means that gulp
will be updated when the version is bumped in the package.json, so everyone will be using the same version of gulp when developing with your project.
gulp 在全局使用时似乎有一些不寻常的行为.当用作全局安装时,gulp 会查找本地安装的 gulp 以传递控制权.因此,gulp 全局安装需要 gulp 本地安装才能工作.上面的答案仍然有效.本地安装总是优于全局安装.
It appears that gulp has some unusual behaviour when used globally. When used as a global install, gulp looks for a locally installed gulp to pass control to. Therefore a gulp global install requires a gulp local install to work. The answer above still stands though. Local installs are always preferable to global installs.
相关文章