有条件地包含依赖 package.json [英] Conditionally Include Dependency package.json
问题描述
我在 npm 项目中有一个仅 Mac OS 需要的依赖项,我想知道是否有某种方法可以在兼容平台是运行 npm install
的平台时有条件地包含此依赖项.
I have a dependency that is only needed for Mac OS in an npm project and was wondering if there is some way to conditionally include this dependency only when the compatible platform is the one running npm install
.
我愿意为此编写逻辑.在以下情况下,grunt-appdmg
导致 npm 安装过程出错(出于相当明显的原因):
I'm willing to write the logic for this. In the below case grunt-appdmg
is causing the npm install process to error out (for fairly obvious reasons) with:
'/dev/cuttle/node_modules/grunt-appdmg/node_modules/appdmg/node_modules/ds-store/node_modules/macos-alias/build'
CXX(target) Release/obj.target/volume/src/volume.o
../src/volume.cc:9:2: error: #error This platform is not implemented yet
#error This platform is not implemented yet
package.json
package.json
{
"name": "Cuttle",
"homepage": "https://github.com/oakmac/cuttle",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/oakmac/cuttle.git"
},
"dependencies": {
"fs-extra": "0.16.3",
"open": "0.0.5",
"winston": "0.8.3"
},
"devDependencies": {
"grunt": "0.4.5",
"grunt-contrib-less": "0.11.4",
"grunt-contrib-watch": "0.6.1",
"grunt-curl": "2.0.3",
"grunt-download-atom-shell": "0.10.0",
"grunt-appdmg": "0.2.0",
"winresourcer": "0.9.0",
"moment": "2.9.0",
"shelljs": "0.3.0"
}
}
推荐答案
您可以使用可选的依赖项.
You can use an optional dependency.
像这样在你的 package.json 中:
Like this in your package.json:
"optionalDependencies":{
"grunt-appdmg":"0.2.0"
}
有关NPM 文档
npm install
如果失败,将跳过它.
npm install
will then just skip it if it fails.
这篇关于有条件地包含依赖 package.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!