Replace an Unused Dependency

Posted

I recently ran into a bit of an odd situation regarding a problematic npm dependency. Our app depended on an old version of d3, which had a dependency on an old version of jsdom, which itself depended on contextify. contextify is not supported on modern versions of Node and would fail to install. Upgrading d3 to a modern version without the dependency on jsdom was too hard, but we needed some way to move forward.

As it turns out, jsdom was only a dependency of d3 in order to support a Node environment, which was not necessary for our app’s use case. Could we replace the jsdom entirely with some kind of “dummy” package, since we didn’t need a real, working version of jsdom anyway?

I took to Twitter with the question, and Jan Buschtöns replied with a great suggestion:

@alexlafroscia You could try creating noop package in your repo and reference it in the resolution using the file: protocol.

As our application is already using yarn workspaces, this worked great! We created a package in the monorepo called noop with nothing but a package.json like this:

{
  "name": "noop",
  "version": "1.0.0"
}

and then used yarn resolutions to point jsdom to that package. Our “root” package.json got the following

{
  "resolutions": {
    "**/d3/jsdom": "file:./packages/noop"
  }
}

which tells yarn to replace the d3 dependency on jsdom with our dummy package.

If you end up in a case like this yourself and don’t have a place to create your own dummy package, you could use something like the none package instead for the same effect!.

WebMentions