[英]Is it possible to install npm package only if it has not been already installed?
Is it possible to install npm
package only if it has not been already installed? 是否可以仅在尚未安装
npm
软件包的情况下安装它?
I need this to speed up test on CircleCI, but when I run npm install protractor@2.1.0
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. 我需要这个来加速对CircleCI的测试,但是当我运行
npm install protractor@2.1.0
等时,它总是下载并从scracth安装它们,但是,在运行命令时,所有模块的node_modules
文件夹已经存在(从以前的版本缓存)和protractor --version
等显示了所需的包版本。
Its perfect to have some one-line command like this: 它是完美的有这样的一行命令:
protractor --version || npm install -g protractor@2.1.0
but the one that will also check version of the package. 但是那个也会检查包版本的那个。
用bash你可以做到
[ $(node -p "require('protractor/package.json').version") != "2.1.0" ] && npm install protractor@2.1.0
You could try npm list protractor || npm install protractor@2.1.0
你可以试试
npm list protractor || npm install protractor@2.1.0
npm list protractor || npm install protractor@2.1.0
Where npm list protractor
is used to find protractor
package. 其中
npm list protractor
用于查找protractor
包。
If the package is not found, it will return npm ERR! code 1
如果找不到包,它将返回
npm ERR! code 1
npm ERR! code 1
and do npm install protractor@2.1.0
for installation npm ERR! code 1
并执行npm install protractor@2.1.0
进行安装
Function version of the excellent answer by @JeromeWAGNER : @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: 要将其他选项传递给
npm
,请在版本之后指定它们,如下所示:
$ install_package_if_needed protractor 2.1.0 -g
[ $(node -p "try{require('protractor/package.json').version}catch(e){}") != "2.1.0" ] && npm install grunt
I had this same issue together with wanting to install global dependencies from any "package.json" file requiring them. 我有同样的问题,想要从任何需要它们的“package.json”文件安装全局依赖项。
This is for a Windows development environment. 这适用于Windows开发环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.