308

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?

danday74
  • 52,471
  • 49
  • 232
  • 283
Stepan Suvorov
  • 25,118
  • 26
  • 108
  • 176
  • 12
    The project's own ["Getting Started" page](https://github.com/gulpjs/gulp/blob/master/docs/getting-started.md#getting-started) says the same thing. (Also doesn't say why.) – T.J. Crowder Mar 01 '14 at 14:15
  • 11
    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 Jun 20 '14 at 22:39
  • @Ciantic No guarantes, but... ➪ http://stackoverflow.com/a/25879563/444255 – Frank N Feb 02 '16 at 13:30

7 Answers7

250

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.

qubyte
  • 17,558
  • 3
  • 30
  • 32
  • 3
    Yes but what when you dont have internet access? How can you use gulp if it's not globaly installed? – IGRACH Aug 14 '14 at 06:25
  • 3
    @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`. – qubyte Aug 15 '14 at 13:47
  • 1
    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. – vesse Sep 08 '14 at 03:23
  • 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! – yeelan Feb 08 '15 at 17:19
  • You might need to open a new question for that one. – qubyte Feb 18 '15 at 21:27
  • But why do you need to install gulp locally if you already have it installed globally? – Alvin Thompson Apr 21 '15 at 19:52
  • _You_ don't, but how do you know that whoever is installing your module or application also has gulp installed globally? If it's in the package.json (installed locally) then you don't have to worry about that. – qubyte Apr 21 '15 at 20:39
  • I also have the same problem as @yeelan. Unless I install in locally, it will give an error "Local gulp not found in". Why? I have installed it globally, and that should work, correct? – CtheGood May 12 '15 at 22:20
  • You should open a new question for that, since it's not directly related to this one. @CtheGood – qubyte May 13 '15 at 09:58
  • `sudo npm` isn't usually needed for global install either, at is not recommended by `npm`'s creator, you should set right directory ownership instead – Dmitri Zaitsev May 20 '15 at 14:20
  • That's not the default behaviour, or configurable from a project you might have just cloned from github. When everything is local and specified by package.json, you get that for free. – qubyte May 20 '15 at 19:18
  • @qubyte Eh? The question of why running gulp would give you "Local gulp not found" is definitely related to the question "Why do we need to install gulp globally and locally?" – Derek Greer Jun 08 '15 at 23:16
  • Global gulp appears to do something similar to global grunt, and looks for a locally installed gulp to hand off to. However, this does not affect my answer. The global install is not necessary. – qubyte Jun 08 '15 at 23:30
  • @qubyte I consider your answer to be quite good, but my point was that both the questions by yeelan and CtheGood are indeed directly related to and indeed contained within the OP's question. As it stands, your answer only address half of the reason. You answer the question: "Why do I have to install globally if I install locally?", where many will naturally be wondering "Why install locally when I have it installed globally? Isn't that the whole point of a global install?" – Derek Greer Jun 09 '15 at 13:59
  • 1
    Added an addendum. Hopefully that covers the strangeness of gulp. – qubyte Jun 09 '15 at 14:34
  • Problem with no global option is that gulp is slow to install. So if I have 10 modules I want to retrofit with gulp, I'm waiting a long time while running `npm i -D gulp gulp-plugin1 gulp-plugin2 ...`. – vaughan Oct 19 '15 at 13:42
  • also to note: if you have to run `sudo` to use `npm -g` you should probably own your npm directory and fix that issue. http://stackoverflow.com/questions/16724259/npm-command-sudo-or-not – Sgnl Oct 28 '15 at 21:46
  • here is npm 8 inside docker with volumes & for whom seeing this answer `build` your image first thin configure your `CMD` or `ENTRYPOINT` to follow any of the two ways @qubyte discriped here - Many thanks man you saved my day – Abd-Elaziz Sharaf May 05 '22 at 15:30
88

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.

Dwayne Crooks
  • 2,737
  • 25
  • 26
  • 1
    ~/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. – mikemaccana Feb 16 '16 at 11:33
  • 3
    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. – ntd Nov 14 '18 at 21:12
82

You can link the globally installed gulp locally with

npm link gulp
Berislav Lopac
  • 16,656
  • 6
  • 71
  • 80
72

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

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

  2. 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:

  1. Including the dependencies of your project locally ensures the version of gulp (or other dependencies) used is the originally intended version.
  2. 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.
  3. 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?

  1. 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.

Steve Bennett
  • 114,604
  • 39
  • 168
  • 219
Derek Greer
  • 15,454
  • 5
  • 45
  • 52
  • 8
    +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?". – Nathan J.B. Jan 06 '16 at 21:34
  • 10
    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. – Kokodoko Nov 15 '16 at 14:39
  • 4
    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 Apr 18 '17 at 16:25
  • 2
    @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. – Stoffe Jul 04 '17 at 17:29
  • 2
    It's really a non-issue now as the community has moved on to just using yarn :) – Derek Greer Jul 07 '17 at 00:15
  • 2
    In my opinion all 3 points that tries to justify a local installation range from sad to plain wrong. `gulp` is a developer tool, not a hard project dependency. I do not ship `git` with my repositories, or `composer` with my server app! – ntd Nov 14 '18 at 21:19
  • I agree that the setup is a bit bizarre. I've worked with a number of build technologies over the years and never recall needing to rely upon different versions for different projects. That aside, any tool required to build the project is a project dependency. Comparing it to a version control tool isn't a good analogy because a project's source rarely has any dependency upon how it's being versioned to build a distribution – Derek Greer Nov 15 '18 at 23:29
8

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.

robrich
  • 13,017
  • 7
  • 36
  • 63
2

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.

tschoartschi
  • 1,453
  • 2
  • 14
  • 23
2

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.

Elliot Nelson
  • 11,371
  • 3
  • 30
  • 44