Install only one package from package.json?

As a workaround you may use something like:

$ node -pe "require('./package').dependencies.bower"
// → 1.0.0
$ npm install bower@$(node -pe "require('./package').dependencies.bower")
// npm install [email protected]

// or with jq
$ npm install bower@$(< package.json jq -r '.dependencies.bower')

Where -e/--eval flag evaluates passed string and -p/--print prints result of eval.


💡 Please consider other answers as well since this one may be outdated.


As @atalantus noted in comment, the accepted answer doesn't work on newer version of NPM. Working solution for newer versions (verified on NPM 6.13.4) is:

npm install --no-package-lock --no-save [email protected]

This will install bower and all its dependencies, but prevents installation of anything else you might have in package.json. It also doesn't create or modify existing package-lock.json.

From npm documentation:

The --no-package-lock argument will prevent npm from creating a package-lock.json file. When running with package-lock's disabled npm will not automatically prune your node modules when installing.

--no-save: Prevents saving to dependencies.

Combining this with Anton Rudeshko's approach to find out version in package.json, the final solution is:

VERSION_BOWER=`node -p -e "require('./package.json').dependencies.bower"`
npm install --no-package-lock --no-save bower@"$VERSION_BOWER"

Tags:

Npm