Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally Include Dependency package.json

Tags:

node.js

npm

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.

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

{
  "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"
  }
}
like image 287
comamitc Avatar asked Feb 25 '15 02:02

comamitc


2 Answers

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
like image 179
redaxmedia Avatar answered Oct 04 '22 13:10

redaxmedia


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.

like image 36
juunas Avatar answered Oct 04 '22 14:10

juunas