Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 751
Stepan Suvorov Avatar asked Mar 01 '14 14:03

Stepan Suvorov


People also ask

Does gulp need to be installed globally?

Gulp comes in two parts: the Gulp CLI (command line utility) that is installed globally on your computer, and then local Gulp packages installed in each individual project you might have. This separation, introduced in Gulp v4 allows you to run different versions of Gulp in each local project.

What is gulp install?

Automatically install npm, bower, tsd, typings, composer and pip packages/dependencies if the relative configurations are found in the gulp file stream respectively. Primary objective. Installation. For global use with slush.

Why do we use gulp?

Gulp is a tool that helps you out with several tasks when it comes to web development. It's often used to do front end tasks like: Spinning up a web server. Reloading the browser automatically whenever a file is saved.

Where is gulp installed?

You can check where it is by typing in npm root -g in the command line. For Mac users, if the root command returns a directory like /Users/YOURNAME/node_modules then this will cause the command not found error. Then you should be able to install the Gulp CLI globally and Gulp locally.


Video Answer


2 Answers

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.

like image 56
qubyte Avatar answered Oct 09 '22 10:10

qubyte


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.

like image 23
Dwayne Crooks Avatar answered Oct 09 '22 09:10

Dwayne Crooks