Why Do We Need to Install Gulp Globally and Locally

Why do we need to install gulp globally and locally?

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.

Why should gulp be installed both globally and locally?

You install gulp globally for using simple gulp command in your terminal and install gulp locally (with package.json dependency) in order not to lose the dependency, because you can install your project to any computer, call npm i and access gulp with ./node_modules/.bin/gulp without any additional installations

Installing gulp plugins locally vs globally

For plugins that you are using via gulp, what you have to do is install them locally to the project. Although you have to install gulp itself globally and locally, to run the file and then for the project to pick up the gulp based commands and functions.

The reason you should install the plugins via npm locally is so that it is specific to the project, for example if you then went to upload this project to your server or host on github then you would have to then go and have to globally install all of your packages again. If they are saved, they exist in your packages.json, this is so that when you go to run npm install to install all the packages for said project npm knows what to install.

If I can further clarify anything let me know.

gulp installation global vs. local as dev dependency

The error means you didn't install gulp locally. This means you have to add it to your dependencies in package.json (or just call npm i gulp --save).

It needs to be installed locally because gulpfile.js typically runs some code related to gulp. That's why it calls var gulp = require('gulp'); at the top of your gulpfile.js. This call loads gulp from your package node_modules. That's also where functions like gulp.task or gulp.src come from.

At the same you want to easily use gulp in CLI, that's why it needs to be installed also globally so you can run it by just:

$ gulp

Btw, you can also run just your local gulp:

  1. Insert to your package.json:

    "scripts": {
    "gulp": "gulp",
    }

    This tells npm that by executing gulp command we want to run script ./node_modules/.bin/gulp.

  2. Run (you'd have to do this in all projects):

    $ npm run gulp

So it's definitely easier to install it globally.

Can I install just Gulp globally?

Gulp needs to be installed locally, but you can link the local install to a global install:

npm install --global gulp
npm link gulp

See also https://stackoverflow.com/a/30742196/451480

When performing a global install of Gulp, should you install gulp or gulp-cli?

They are two different modules, gulp is intended to be installed locally for the project, and gulp-cli globally, this allows you to use different versions of gulp for different projects.

It's mostly a legacy thing, in the past there were no gulp-cli. The gulp team got more knowledge and decided to split it up.

It's recommended to use gulp-cli globally, and gulp locally.

And the Getting Started documentation also prompts you, to delete any globally installed gulp module.

There is no difference between --global and -g the latter is just a shorthand flag.

gulp - do i have to install plugins for every project?

No. You basically have these options:

npm install pkg           // install package locally
npm install -g pkg // install globally
npm install pkg --save // install package locally and save to package.json
npm install -g pkg --save // install package globally and save to package.json

what does gulp-cli stands for?

The goal of gulp-cli is to let you use gulp like a global program, but without installing gulp globally.

For example if you installed gulp 3.9.1 globally and your project testGulp4 has gulp 4.0 installed locally, what would happen if you run gulp -v into testGulp4?

  • Without gulp-cli globally installed :

    CLI version 3.9.1

    In this case the version displayed is the global version of gulp. The local version 4.0 is totally ignored.

  • With gulp-cli globally installed :

    CLI version 1.2.1
    Local version 4.0.0-alpha.2

    In this case the version displayed is the global version of gulp-cli and the local version of gulp. The global gulp 3.9.1 is totally ignored.

Conclusion :

  • gulp-cli: is preferred because it allows you to use different versions of gulp.
  • gulp: needs a local version of gulp installed.


Related Topics



Leave a reply



Submit