One can insist to automatically npm-install a package globally.
- Add
install-g
to yourpackage.json
dependencies. - Add
"scripts": { "install": "node_modules/.bin/install-g || true" }
As a result, any package using your package as a dependency would trigger its global installation. Use of devDependencies isn't recommended, unless your project is private. If you publish it on npm and others end up depending on it -- it will not install automatically...
Because the install script is followed by || true
, non-zero exitcodes will be ignored.
Thus a missing ./node_modules/.bin/install-g
won't be a problem. Nor will other possible errors
such as the currently usupported sudo
cause the install to fail. The error will still print
to stderr, but a failed global install will not break the local install.
I'm not sure if Windows can take the || true
though.
Also note the use of install
(i.e. post-install
) rather than pre-install
.
If people run install -g
on your package it will install once rather than twice.
Need not ask "please install {something} globally" - it just happens... See install-g's dependents for example.
batshit test