ChatGPT解决这个技术问题 Extra ChatGPT

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. Why do I need this?

The project's own "Getting Started" page says the same thing. (Also doesn't say why.)
I wish npm could use globally installed a dependency package that is same version as local package. 5MB of glup stuff for each project directory :/
@Ciantic No guarantes, but... ➪ stackoverflow.com/a/25879563/444255

q
qubyte

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+

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

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.

So, if you are happy with (in your 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.

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.

Addendum:

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.


Yes but what when you dont have internet access? How can you use gulp if it's not globaly installed?
@IGRACH The above script does not use an internet connection. If you want to do the same thing without using a script field in package.json, then use ./node_modules/.bin/gulp.
I have defined aliases for gulp and coffee so the commands work from my node project root (eg. alias gulp="node_modules/.bin/gulp"). This way the commands are easy to use if needed and global/local version conflicts do not occur.
Thanks @qubyte! I think install it locally is a good practice in general. I got one more question so hope you can help me clear my mind. I tried install it globally as Gulp's document suggested without install it locally. So when I try to run gulp, it gives me the following error message Local gulp not found in .... As far as I understand, it should first look at local node_modules and if not found it should then look into globally installed modules, aren't it? Thanks!
Added an addendum. Hopefully that covers the strangeness of gulp.
D
Dwayne Crooks

TLDR; Here's why:

The reason this works is because gulp tries to run your gulpfile.js using your locally installed version of gulp, see here. Hence the reason for a global and local install of 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. By installing it globally the gulp script gets into your PATH because the global node/bin/ directory is most likely on your path.

To respect your local dependencies though, gulp will use your locally installed version of itself to run the gulpfile.js.


~/bin is a Unix convention for per-user binaries, and in PATH by default on many OSs. gulp should be able to link its binary from there.
Said in other words your globally installed gulp package is needed for putting node_modules/.bin/gulp in path. Storage is cheap but throwing away MB for simulating a symlink is IMO pure sloppiness.
B
Berislav Lopac

You can link the globally installed gulp locally with

npm link gulp

I know that it's best to use local installs, but there might be cases where you just can't install it or just don't want to (imagine that your dedicated CI server has gulp globally installed and you are reinstalling it on every commit). Anyway, +1 for mentioning npm link.
I see what you did there. That's clever.
This doesn't attempt to answer the question
No, it just invalidates it.
S
Steve Bennett

The question "Why do we need to install gulp globally and locally?" can be broken down into the following two questions:

Why do I need to install gulp locally if I've already installed it globally? Why do I need to install gulp globally if I've already installed it locally?

Several others have provided excellent answers to theses questions in isolation, but I thought it would be beneficial to consolidate the information in a unified answer.

Why do I need to install gulp locally if I've already installed it globally?

The rationale for installing gulp locally is comprised of several reasons:

Including the dependencies of your project locally ensures the version of gulp (or other dependencies) used is the originally intended version. Node doesn't consider global modules by default when using require() (which you need to include gulp within your script). Ultimately, this is because the path to the global modules isn't added to NODE_PATH by default. According to the Node development team, local modules load faster. I can't say why this is, but this would seem to be more relevant to node's use in production (i.e. run-time dependencies) than in development (i.e. dev dependencies). I suppose this is a legitimate reason as some may care about whatever minor speed advantage is gained loading local vs. global modules, but feel free to raise your eyebrow at this reason.

Why do I need to install gulp globally if I've already installed it locally?

The rationale for installing gulp globally is really just the convenience of having the gulp executable automatically found within your system path.

To avoid installing locally you can use npm link [package], but the link command as well as the install --global command doesn't seem to support the --save-dev option which means there doesn't appear to be an easy way to install gulp globally and then easily add whatever version that is to your local package.json file.

Ultimately, I believe it makes more sense to have the option of using global modules to avoid having to duplicate the installation of common tools across all your projects, especially in the case of development tools such as grunt, gulp, jshint, etc. Unfortunately it seems you end up fighting the tools a bit when you go against the grain.


+1 for being the first person on the entire internet to point out that there are two points to the question. Most everyone everywhere just answers "Why do I need to install gulp globally if I've already installed it locally?" when what I wanted to know was "Why do I need to install gulp locally if I've already installed it globally?".
The fact that this question needs such elaborate explanation means that this simply isn't a very logical way of working. Installing the same tool over and over again for each project shouldn't be necessary.
Your answer is so beautiful unemotional. Mine would have had 80% swearwords, as this seems to be soo ******* stupid. From the tooling perspective the local installation theory is probably right but from an OS perspective and a package managers perspective this is so crazy I can't find words for it. What drugs do the NPM/gulp guys take?!? If anybody disagrees, please read how system package manager like dpkg, yum, pacman and co. work.
@JepZ it's only gulp being super weird though, there's nothing in node or npm forcing this. And keeping specific versions of gulp in the project only makes sense if the gulp guys break patch versions on a regular basis or something, other build tools are usually a global install. But ah well. Just here for the swearing.
It's really a non-issue now as the community has moved on to just using yarn :)
r
robrich

Technically you don't need to install it globally if the node_modules folder in your local installation is in your 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.

I've never installed gulp globally -- I think it's bad form.


Better approach than putting it in your path is to use NPM scripts
t
tschoartschi

I'm not sure if our problem was directly related with installing gulp only locally. 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. 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. 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. 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.


Yes, please, dive deeper into this issue.
E
Elliot Nelson

Just because I haven't seen it here, if you are on MacOS or Linux, I suggest you add this to your PATH (in your bashrc etc):

node_modules/.bin

With this relative path entry, if you are sitting in the root folder of any node project, you can run any command line tool (eslint, gulp, etc. etc.) without worrying about "global installs" or npm run etc.

Once I did this, I've never installed a module globally.