Saturday, October 21, 2017

How can I force npm to resolve a dependency's dependency to a different package?

Leave a Comment

TL;DR: How can I change one of my package's dependency's dependencies to a different package? For instance, I want to change Package A's dependency Package B to be Package C, but only for Package A (i.e. I don't want to change Package A's dependencies upstream).


I'm writing a plugin for Leaflet. Leaflet is available as an NPM package (and my plugin will be, too, when I'm finished). There's also another plugin my plugin extends, Esri-Leaflet, that has Leaflet as a dependency.

My plugin uses Mocha/Chai/Sinon as a test framework for my code. I run these tests with an NPM script both during development and as a part of CI.

When I run tests that depend upon Leaflet I have a number of errors because Leaflet, unfortunately, depends upon a number of globals not available in a headless node environment (such as window). Fortunately, there is an alternative package that was suggested to me called leaflet-headless that shims over those problems (it's actually pretty interesting to see if you're curious).

Is there a way to, just for my package, tell NPM to use leaflet-headless intead of Leaflet when resolving Esri-Leaflet's dependencies? That is, I either want to remove the downstream Leaflet dependency (because my project already depends upon leaflet-headless) or change it to be leaflet-headless.

I've looked at npm shrinkwrap, but it seems like it can only specify version numbers, not change actual package dependencies.

If there is no functionality with NPM to do what I'm asking, can you recommend an alternative solution? I'm already exploring fixing the reliance upon globals in Leaflet upstream in order to eliminate the need for leaflet-headless.

1 Answers

Answers 1

This may or may not work depending on your version of Node, but I was able to accomplish something similar on Node 8 with a same-named package.

If you need to make the package the same name, you can fork the project, and rename the fork to be the same (in this case leaflet-headless -> Leaflet).

After you have your fork, write the following in your package.json

dependencies: {     "Leaflet": "username/Leaflet" } 

If you have a package-lock.json, you should see Esri-Leaflet point to your github, and not the original Leaflet project as a dependency.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment