DEV Community

Stefan Judis
Stefan Judis

Posted on • Originally published at stefanjudis.com

4

How to override your dependency's dependencies

npm released version 8.3 of their CLI client in December and it looks like an unspectacular release but includes a helpful new feature - "overrides".

The JavaScript ecosystem has been on fire since Node.js and npm appeared. There's always a package for everything, and people YOLO-publish whatever they please. It's a vibrant and enabling ecosystem feeling like the wild wild west. And of course, there are pros and cons to countless dependencies.

I love that I can "just install another package" but share the concerns about the increasing project complexity. Suppose your project relies on one dependency that depends on another dependency that again depends on another one just to add two numbers. In that case, countless things could go wrong. "npm overrides" give you more power over what's installed in your dependency tree.

Let's say one of your dependencies (1st level) relies on another dependency that includes outdated other dependencies (2nd level). There hasn't been an easy way to update dependencies down the tree other than forking and fixing your first-level dependency.

your-project
  |_ some-module @1.0.0
      |_ another-module-which-should-be-updated @1.0.0
Enter fullscreen mode Exit fullscreen mode

You can now specify an overrides property in your package.json to override and enforce dependency versions in the tree.

{
  "overrides": {
    "bar@2.0.0": {
      "foo": "1.0.0"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The new feature comes in handy to

  • patch a dependency with a known security issue
  • replace an existing dependency with a fork
  • make sure that the same package version is used everywhere.

It's such a welcome addition; thanks, npm! 🎉

Read more about it in the npm docs.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay