I'd prefer to type a short command, like npm install -g
, to setup a project's global dependencies, such as node-sass and jshint, than manually typing out npm install -g every single package
. Is there an npm-idiomatic way to do this?

- 681
- 2
- 8
- 17
-
3There isn't really one. `dependencies` and akin are for packages you intend to `require()`. Global packages you intend to execute from a terminal/shell/etc. are outside the scope of an individual project. You can however install them locally and still execute them: `$ ./node_modules/node-sass ...` – Jonathan Lonowski Aug 06 '13 at 21:17
-
1Sorry. The last bit should be `$ ./node_modules/.bin/node-sass ...` – Jonathan Lonowski Aug 06 '13 at 22:38
-
2related : http://stackoverflow.com/questions/6480549/install-dependencies-globally-and-locally-using-package-json – nha Feb 22 '14 at 14:50
-
@rha's pointer is still good. That said, I guess a simple script could be written that uses `jq` (?) to extract `devDependencies` and loop over `npm i -g` calls. – Raphael May 29 '20 at 09:34
2 Answers
You are using npm install -g <pkg>
wrong here. -g
indicates, that it's no project dependencies, than rather you global (PC wide).
Those plugins are no devDependencies, but CLI runners. What you want is npm install --save-dev every single package
upon initialisation. When you need to install those dependencies again you'd just run npm install
and include something like ./node_modules/.bin/jshint
to your package.json
scripts in order not to depend on the CLIs.

- 6,304
- 7
- 29
- 61
I get you are not supposed to do it, but if you still want it, change your global location to make sure you have permissions. Install jq (either download it, or apt install jq) and then:
export NPM_CONFIG_PREFIX=~/npm-global
cat ./package.json | jq '.devDependencies | keys[] as $k | "\($k)@\(.[$k])"' | xargs -t npm install --global
This will create a list of packages and versions from the devDependencies section, pipe that into xargs, and call npm install with them.

- 2,526
- 22
- 26