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 inst
Function version of the excellent answer by @JeromeWAGNER:
function install_package_if_needed() {
local p=${1:-Package required}
local v=${2:-Version required}
shift 2
local i=$(node -p "require('$p/package.json').version" 2>/dev/null)
[ "$i" == "$v" ] || npm "$@" install "$p@$v"
}
Use like:
$ install_package_if_needed protractor 2.1.0
To pass additional options to npm, specify them after the version, like so:
$ install_package_if_needed protractor 2.1.0 -g