IT:AD:npm:HowTo:When To Install Packages Globally (Rarely!)
Summary
A lot of packages demonstrate installing globally:
- install globally
npm install xyz -G
Please stop and consider what you are doing before you install Globally.
Why You Shouldn't Use Global
There are several issues with installing packages globally:
- Can require elevated privileges.
- Can stop working. Consider a case where you were working on the project, stop for 6 months, work on something else that required an update of gulp or other dependency, come back…nothing works.
- You share your package. They use a different version of the global package…nothing works for them.
When to Use Global
The golden rule as to when to choose Global is when you want to invoke it from the command line, and choose local if you depend on the package from your own module (using require).
But it's not always that easy to know where the cut is.
Some packages (eg: yeoman) are clearly command line tools, that won't be invoked by your grunt/gulp workflows.
But others (eg: older versions of express) should be local, but can also be invoked from the command line…
* https://www.quora.com/When-do-I-install-a-package-locally-or-globally-in-NPM
In such cases, install both. Install globally first, then install the dependency locally.
Use --save-dev (-D)
If the package is a built tool, install them using the -D flag:
install uglify-js -D
What this does is still install the package locally, in the same local node_modules directory – but it marks the package not in the dependencies part of the package.json, but in the devDependencies section:
- package.json
{ "name": "example", "version": "1.0.0", ... "dependencies": {} ... "devDependencies": { "uglify-js": "^2.6.2" }, }
Some Examples
| Package | Global (-g) | Local | Dev (-D) | Notes | |
|---|---|---|---|---|---|
| yeoman | x | ||||
| grunt-cli | x | ||||
| grunt | x | ||||
| gulp-cli | x | ||||
| gulp | x | ||||
| gulp-watch | x | ||||
| uglify-js | x | ||||
| bower | x | It's a client side package manager | |||
| gulp-ruby-sass | x | ||||
| gulp-autoprefixer | x | ||||
| gulp-cssnano | x | ||||
| gulp-jshint | x | ||||
| gulp-concat | x | ||||
| gulp-uglify | x | ||||
| gulp-imagemin | x | ||||
| gulp-notify | x | ||||
| gulp-rename | x | ||||
| gulp-livereload | x | ||||
| gulp-cache | x | ||||
| del | x | (It's also a gulp module) |