本文翻译自:Why do we need to install gulp globally and locally?
2 manuals about gulp say that I need to install gulp first globally (with -g flag) and then one more time locally. 2关于gulp的手册说我需要首先全局安装gulp(使用-g标志)然后再在本地安装一次。 Why do I need this? 我为什么需要这个?
#1楼
参考:https://stackoom.com/question/1uNe0/为什么我们需要在全球和本地安装gulp
#2楼
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. 节点项目的全局安装很糟糕,因为它们使部署更加困难。
npm 5.2+ npm 5.2+
The npx
utility bundled with npm
5.2
solves this problem. 与npm
5.2
捆绑在一起的npx
实用程序解决了这个问题。 With it you can invoke locally installed utilities like globally installed utilities (but you must begin the command with npx
). 有了它,您可以调用本地安装的实用程序,如全局安装的实用程序(但必须使用npx
启动命令)。 For example, if you want to invoke a locally installed eslint
, you can do: 例如,如果要调用本地安装的eslint
,可以执行以下操作:
npx eslint .
npm < 5.2 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的script
字段中使用时, npm
node_modules
中搜索工具以及全局安装的模块,因此本地安装就足够了。
So, if you are happy with (in your package.json): 所以,如果你满意(在你的package.json中):
"devDependencies": {
"gulp": "3.5.2"
}
"scripts": {
"test": "gulp test"
}
etc. and running with npm run test
then you shouldn't need the global install at all. 等等并运行npm run test
然后你根本不需要全局安装。
Both methods are useful for getting people set up with your project since sudo
isn't needed. 由于不需要sudo
这两种方法对于让人们设置项目非常有用。 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. 这也意味着当package.json中出现版本时, gulp
将会更新,因此在使用您的项目进行开发时,每个人都将使用相同版本的gulp。
Addendum: 附录:
It appears that gulp has some unusual behaviour when used globally. 看来gulp在全球使用时有一些不寻常的行为。 When used as a global install, gulp looks for a locally installed gulp to pass control to. 当用作全局安装时,gulp会查找本地安装的gulp以将控制权传递给。 Therefore a gulp global install requires a gulp local install to work. 因此,gulp全局安装需要gulp本地安装才能工作。 The answer above still stands though. 上面的答案仍然有效。 Local installs are always preferable to global installs. 本地安装始终优于全局安装。
#3楼
Technically you don't need to install it globally if the node_modules
folder in your local installation is in your PATH
. 从技术上讲,如果本地安装中的node_modules
文件夹位于PATH
,则无需全局安装。 Generally this isn't a good idea. 一般来说,这不是一个好主意。
Alternatively if npm test
references gulp
then you can just type npm test
and it'll run the local gulp. 或者,如果npm test
参考gulp
然后你可以输入npm test
并且它将运行本地gulp。
I've never installed gulp globally -- I think it's bad form. 我从来没有在全球范围内安装gulp - 我认为这是糟糕的形式。
#4楼
TLDR; TLDR; Here's why : 原因如下:
The reason this works is because
gulp
tries to run yourgulpfile.js
using your locally installed version ofgulp
, see here . 这部作品的原因是因为gulp
试图运行gulpfile.js
使用本地安装的版本gulp
,看这里 。 Hence the reason for a global and local install of gulp. 因此全球和本地安装gulp的原因。
Essentially, when you install gulp
locally the script isn't in your PATH
and so you can't just type gulp
and expect the shell to find the command. 本质上,当你在本地安装gulp
,脚本不在你的PATH
,所以你不能只输入gulp
并期望shell找到命令。 By installing it globally the gulp
script gets into your PATH
because the global node/bin/
directory is most likely on your path. 通过全局安装, gulp
脚本会进入您的PATH
因为全局node/bin/
目录很可能在您的路径上。
To respect your local dependencies though, gulp
will use your locally installed version of itself to run the gulpfile.js
. 为了尊重您的本地依赖关系, gulp
将使用您自己的本地安装版本来运行gulpfile.js
。
#5楼
I'm not sure if our problem was directly related with installing gulp only locally. 我不确定我们的问题是否与本地安装gulp直接相关。 But we had to install a bunch of dependencies ourself. 但我们必须自己安装一堆依赖项。 This lead to a "huge" package.json and we are not sure if it is really a great idea to install gulp only locally. 这导致了一个“巨大的”package.json,我们不确定在本地安装gulp是否真的是一个好主意。 We had to do so because of our build environment. 由于我们的构建环境,我们必须这样做。 But I wouldn't recommend installing gulp not globally if it isn't absolutely necessary. 但是如果不是绝对必要的话,我不建议不要全局安装gulp。 We faced similar problems as described in the following blog-post 我们遇到了类似的问题,如以下博客文章中所述
None of these problems arise for any of our developers on their local machines because they all installed gulp globally. 我们的本地计算机上的任何开发人员都没有出现这些问题,因为他们都在全球安装了gulp。 On the build system we had the described problems. 在构建系统上,我们遇到了所描述的问题。 If someone is interested I could dive deeper into this issue. 如果有人有兴趣,我可以深入研究这个问题。 But right now I just wanted to mention that it isn't an easy path to install gulp only locally. 但是现在我只想提一下,在本地安装gulp并不是一条简单的途径。
#6楼
您可以在本地链接全局安装的gulp
npm link gulp