Is it possible to install npm package only if it has not been already installed?

kovpack picture kovpack · Jun 5, 2015 · Viewed 22.3k times · Source

Is it possible to install npm package only if it has not been already installed?

I need this to speed up test on CircleCI, but when I run npm install [email protected] etc. it always downloads things and installs them from scracth, however, node_modules folder with all modules is already present at the moment of running commands (cached from previous build) and protractor --version etc. shows the needed version of the package.

Its perfect to have some one-line command like this:

protractor --version || npm install -g [email protected]

but the one that will also check version of the package.

Answer

An Nguyen picture An Nguyen · Aug 23, 2017

You could try npm list protractor || npm install [email protected]

Where npm list protractor is used to find protractor package.

If the package is not found, it will return npm ERR! code 1 and do npm install [email protected] for installation