Conditionally Include Dependency package.json

前端 未结 2 380
北荒
北荒 2021-01-02 10:18

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

相关标签:
2条回答
  • 2021-01-02 10:34

    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. 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:

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

    Install the lintDependencies:

    handpick --target=lintDependencies
    

    Install the devDependencies and lintDependencies via YARN:

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

    Install the devDependencies without testDependencies:

    handpick --target=devDependencies --filter=testDependencies
    

    Install the dependencies and devDependencies within path:

    handpick --path=../shared
    
    0 讨论(0)
  • 2021-01-02 10:46

    You can use an optional dependency.

    Like this in your package.json:

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

    More info on NPM documentation

    npm install will then just skip it if it fails.

    0 讨论(0)
提交回复
热议问题