1 min read

Bug in dependency or module resolution issues? Debugging with Selective Dependency Resolutions

When multiple versions of a dependency are used in your Node project and you start seeing build or import errors related to that dependency, it can be frustrating to solve.

I recently learned about selective dependency resolutions (SDR) and it has since made me more confident in debugging these types of errors. SDR makes it easier to rule out if the error(s) I'm seeing are due to a newer version of a dependency (maybe a bug or a change in API in that version?) or if there's an issue with module resolution in my project.

How do I use it?

By adding a resolutions object in your package.json, you can specify the version of a dependency to be used by all consumers of that package in the project. This includes uses of that dependency within other dependencies.

	"resolutions": {
	    // This pins all uses of 'some-dependency' to '1.0.0'
        // instead of what's listed in the 'dependencies' or 
        // 'devDependencies' sections
    	"some-dependency": "1.0.0",
        "another-dependency": ">=1.0.0 || 2.0.0"

Which package managers have this?

Yarn and pnpm both have support for the resolutions field, and npm achieves this through the overrides field.

Additional resources

If you'd like to read more about this, Yarn's documentation is great. They have a formal specification of the feature as well since I believe they were the first package manager to add this feature.

Enjoy this post? Subscribe to be notified when I publish new content!