Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install only one package from package.json?

Tags:

npm

Suppose that somewhere in my package.json I have:

"dependencies": {
    "bower": "1.0.0",
    // zillion other dependencies
}

Is there a way to make npm install only [email protected] from my package.json? Like so: npm install --only bower.

My goal is to make npm install and bower install run simultaneously.

like image 806
Anton Rudeshko Avatar asked Mar 15 '14 06:03

Anton Rudeshko


People also ask

How do I install a specific version of a package?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.

What is devDependencies in package json?

Dev Dependencies: In package. json file, there is an object called as dev Dependencies and it consists of all the packages that are used in the project in its development phase and not in the production or testing environment with its version number.

What is the difference between dependency and Devdependency?

"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.


2 Answers

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.

like image 82
Anton Rudeshko Avatar answered Sep 24 '22 22:09

Anton Rudeshko


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"
like image 23
Patrik Stas Avatar answered Sep 22 '22 22:09

Patrik Stas