简体   繁体   English

有条件地包含依赖 package.json

[英]Conditionally Include Dependency package.json

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 .我在 npm 项目中有一个仅 Mac OS 需要的依赖项,并且想知道是否有某种方法可以仅在兼容平台是运行npm install平台时有条件地包含此依赖项。

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:在以下情况下grunt-appdmg导致 npm 安装过程出错(出于相当明显的原因):

'/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包.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.您可以使用可选的依赖项。

Like this in your package.json:像这样在你的 package.json 中:

"optionalDependencies":{
  "grunt-appdmg":"0.2.0"
}

More info on NPM documentation有关NPM 文档的更多信息

npm install will then just skip it if it fails.如果失败, npm install将跳过它。

Let me introduce handpick that lets you target and filter multiple dependencies.让我来介绍精选的,可以让你的目标和过滤多个依赖。 I wrote this to speed up CI stages that just need a fragment of the devDependencies but there are eventually more usecases.我写这个是为了加速 CI 阶段,这些阶段只需要 devDependencies 的一个片段,但最终会有更多的用例。 This project is quite experimental - please leave some feedback.这个项目相当实验 - 请留下一些反馈。

Installation安装

Install on your system:在您的系统上安装:

npm install handpick --global

Usage用法

Run the command:运行命令:

handpick [options]

-V, --version
-T, --target
-F, --filter
-M, --manager
-P, --path
-h, --help

Examples例子

Define unofficial dependencies inside package.json file:package.json文件中定义非官方依赖项:

{
    "lintDependencies":
    {
        "eslint": "6.8.0",
        "eslint-config-redaxmedia": "2.0.0"
    },
    "testDependencies":
    {
        "chai": "4.2.0",
        "mocha": "7.1.1"
    }
}

Install the lintDependencies :安装lintDependencies

handpick --target=lintDependencies

Install the devDependencies and lintDependencies via YARN:通过 YARN 安装devDependencieslintDependencies

handpick --target=devDependencies --target=lintDependencies --manager=yarn

Install the devDependencies without testDependencies :安装devDependenciestestDependencies

handpick --target=devDependencies --filter=testDependencies

Install the dependencies and devDependencies within path:在路径中安装dependenciesdevDependencies

handpick --path=../shared

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM