Hey folks, I’m trying to figure out if npm has something like yarn’s resolutions feature. I couldn’t find anything in the npm docs about it.
Here’s what I’m trying to do: I want to install packageA version 2.1.0 and ensure one of its dependencies (packageB) is also at version 2.1.0. With yarn, I can achieve this using the following configuration:
However, I’m using npm and prefer not to manually alter the package-lock.json file. Does npm offer a similar feature or is there a recommended workaround?
hey sofiag, npm doesn’t have a direct equivalent to yarn’s resolutions. but you could try using “npm-force-resolutions” package. it lets u override dependency versions in package.json. just add an “overrides” field:
{
"overrides": {
"packageB": "2.1.0"
}
}
run “npx npm-force-resolutions” before “npm install”. hope this helps!
As someone who’s been in your shoes, I can tell you that managing package versions in npm can be a bit tricky compared to yarn. While npm doesn’t have a direct equivalent to resolutions, I’ve found a workaround that’s been pretty reliable for me.
You can use the ‘pnpm’ package manager, which is compatible with npm projects and offers a feature similar to yarn’s resolutions. It’s called ‘pnpm.overrides’ and works in a very similar way. Here’s what you’d do:
Install pnpm globally: npm install -g pnpm
In your project root, create a ‘pnpm-workspace.yaml’ file
Then use pnpm instead of npm for installing packages. This approach has saved me countless headaches when dealing with complex dependency trees. Give it a shot and see if it works for your use case!
npm doesn’t offer a built-in feature like yarn’s resolutions, but there are workarounds. One approach is to use the ‘overrides’ field in your package.json file, which was introduced in npm version 8.3.0. It allows you to specify version requirements for nested dependencies. Here’s how you could use it:
This method ensures that packageB is installed at version 2.1.0 regardless of what other packages might request. It’s a cleaner solution than manually editing package-lock.json and provides similar functionality to yarn’s resolutions.