RSquared
RSquared

Reputation: 1188

Root package.json unmet dependency in child package.json

Lets say I have a Typescript project like so

root/
  api/
    package.json
  web/
    package.json
  ...
  package.json

In the root package.json I have the typescript dependency installed (this is to ensure it uses the same version in web & api).

If I install a package in web/package.json that has a peerDepedency on Typescript (lets say ts-loader) it will complain that Typescript is an unmet dependency.

root package.json

{
  ...
  "devDependencies": {
    "@types/node": "^11.11.7",
    ...
    "typescript": "^3.3.4000"
  }
}

web/package.json

{
  ...
  "devDependencies": {
    "ts-loader": "^5.3.3",
    "webpack": "^4.29.6"
  }
}

How do I get around this without adding Typescript in the web/package.json?

If thats not possible, how do you ensure that the same version of packages is used across multiple modules?

Upvotes: 2

Views: 2431

Answers (1)

Derek Nguyen
Derek Nguyen

Reputation: 11577

Unfortunately npm doesn't have any sort of package.json inheritance.

Since it's just a warning & everything should still work since node looks for node_modules in parent directories, I think you can do one of these:

  • Just ignore the warning
  • Move ts-loader up to parent directory
  • Declare & install an exact version of typescript in each sub directory (without any modifier) and update them manually whenever necessary
  • Switch to yarn & use workspaces (I think you'd still have to list typescript in each package's dependencies, but at least they will be installed only once.)
  • Use lerna, a js monorepo manager that can manage cross dependencies

Upvotes: 3

Related Questions