Search code examples
node.jsdependencies

NodeJS the devDependencies of my dependencies/devDependencies are not being installed or listed


In my project the devDependencies of my dependencies, or indeed my devDependencies, are not being installed when I run

npm install

or

npm install --include=dev

In many ways this seems sensible. But one of the dependencies, maybe incorrectly, fails when a devDependency is not available.

Is this meant to work? npm does install the sub dependencies it just seems to ignore the devDependencies.

An example of one of my dependenices package.json

{
  "name": "test-helper-chai",
  "version": "1.0.11",
  "description": "chai extended with specific assertions",
  "license": "MIT",
  "main": "dist/index.js",
  "devMain": "src/index.js",
  "engines": {
    "node": ">=14"
  },
  "devDependencies": {
    "chai": "^4.3.4",
    "eslint": "^8.24.0",
    "prettier": "^2.7.1",
    "sinon": "^9.2.4"
  },
  "dependencies": {
    "check-error": "^1.0.2",
    "lodash": "^4.17.21"
  },
  "scripts": {
    "build": "yarn build:src",
    "build:src": "build -dest \"./dist\" -src \"./src\" -js -ts -maps",
    "test": "yarn test:style && yarn test:unit && yarn test:integration && yarn test:browser",
    "test:browser": "test --integration --unit --runner karma",
    "test:integration": "test --integration --runner mocha",
    "test:style": "eslint ./src/**/*.*",
    "test:unit": "test --unit --runner jest"
  }
}

Here chai is not being installed but check-error is.


Solution

  • This is expected behavior.

    When you run npm install in the same directory as a package.json file, npm will install both devDependencies and dependencies for that package.json. It's presumed that package.json is the package under development.

    Nested dependencies work differently. Only the regular dependencies of nested dependencies will get installed. This is to cut down on bloat. Imagine if every package shipped all of their build tools, development packages, and linters along with their production code!

    It's worth keeping in mind for the package you develop. If a dependency is needed for the production package to run, you should install it in dependencies. If it's only needed for build/development, it should go in devDependencies.