Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

npm released version 8.3 of their CLI client in December, and the minor version release looks unspectacular but includes a very helpful new feature - "overrides".

The JavaScript ecosystem has been on fire since Node.js and npm appeared. There's always a package for everything because folks have been YOLO-publishing 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 one that again depends on another. In that case, countless things could go wrong, but the most critical one is when of your dependencies was hacked or does something malicious.

Read more about a recent occasion and the node-ipc incident in March 2022.

npm overrides enable you to control your dependencies' dependencies

"npm overrides" give you more control 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 control nested dependency versions down the node_modules 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

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

{
  "overrides": {
    "bar@2.0.0": {
      "foo": "1.0.0"
    }
  }
}

Above the bar package with the version 2.0.0 would be overriden by foo.

Our friends at Snyk shared a snippet that describes how to override a package with a specific version range. Read the following package.json configuration as:

  • override every node-ipc package larger than 9.2.1 but smaller than 10.
  • override every node-ipc package larger than 10.1.0.
{
  "overrides": {
    "node-ipc@>9.2.1 <10": "9.2.1",
    "node-ipc@>10.1.0": "10.1.0"
  }
}

The new overrides 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.

If you enjoyed this article...

Join 5.3k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles