Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 912
RSquared Avatar asked Nov 26 '25 19:11

RSquared


1 Answers

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
like image 74
Derek Nguyen Avatar answered Nov 29 '25 13:11

Derek Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!