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?
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.
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.
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.
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.
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.
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 .
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.
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.
TLDR; Here's why:
The reason this works is because
gulp
tries to run yourgulpfile.js
using your locally installed version ofgulp
, 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With