Versions forced with npm-force-resolutions come undone when a new package is installed
Your error
npm ls shows the forced version after npm install, and the old version again right after npm install some-package2 more lines it can showFix it fast
Most common cause, 1 of 2The preinstall hook does not run when a package name is given
Replace the tool with npm's own overrides field in the root package.json, which npm consults whenever it builds the dependency tree, named install or not.
// package.json (root)
{
"overrides": {
"hoek": "4.2.1"
}
}
npm pkg delete scripts.preinstall resolutions
npm installShort answer
npm-force-resolutions rewrites package-lock.json from a root preinstall script, and npm runs root install hooks only on a bare npm install. npm install some-package skips the hook, saves a fresh lockfile, and the forced version is gone. npm itself never reads resolutions. Put the pins under overrides in the root package.json and drop the hook (npm 11 and 12).
Which cause do you have?
Look at how the pin is wired, then check the installed version twice: once after a bare install and once after the next named install you run anyway. A pin that holds only after the bare install points to the first cause; a pin that never holds points to the second.
grep -n -A4 -E '"(resolutions|preinstall|overrides)"' package.json
npm install && npm ls hoek
# after your next npm install <name>:
npm ls hoekThen find the line you see in this list and open its fix:
- 1The preinstall hook does not run when a package name is given
npm ls shows the forced version after a bare npm install, and the old one again right after npm install <name> - 2The resolutions block has nothing that reads it
package.json has resolutions but no preinstall hook calling npm-force-resolutions, often after moving a project from Yarn, and npm ls has never shown the forced version
What does the error look like?
npm ls shows the forced version after npm install, and the old version again right after npm install some-packagepackage.json has "preinstall": "npx npm-force-resolutions" and a resolutions block, yet npm audit still flags the nested packageA resolutions block copied from a Yarn project changes nothing under npm
Cause 1 of 2
The preinstall hook does not run when a package name is given
You seenpm ls shows the forced version after a bare npm install, and the old one again right after npm install <name>
Check
npm-force-resolutions works by patching package-lock.json in a root preinstall script before each install. npm documents that the root install hooks, preinstall among them, run on a bare npm install with no package arguments. Installing a named package therefore skips the patch, and npm writes a lockfile that no longer carries the forced version.
npm pkg get scripts.preinstall resolutionsFix
Replace the tool with npm's own overrides field in the root package.json, which npm consults whenever it builds the dependency tree, named install or not. Delete the preinstall hook and the resolutions block in the same change, then install once so the lockfile is rebuilt with the override.
// package.json (root)
{
"overrides": {
"hoek": "4.2.1"
}
}
npm pkg delete scripts.preinstall resolutions
npm installVerify
The pin survives the next named install: after npm install <name>, npm ls still shows the forced version.
npm ls hoek --allConfirmed by docs.npmjs.com
Cause 2 of 2
The resolutions block has nothing that reads it
You seepackage.json has resolutions but no preinstall hook calling npm-force-resolutions, often after moving a project from Yarn, and npm ls has never shown the forced version
Check
resolutions is a Yarn field. npm's package.json reference describes overrides as the way to replace a package anywhere in the tree and defines no resolutions setting, so npm installs as if the block were absent. Only npm-force-resolutions reads that block, and only when its preinstall hook runs.
npm pkg get resolutions overridesFix
Move each entry into overrides. Overrides count only in the root package.json, not in workspaces or installed packages. A pin on a package you also list as a direct dependency must use the same spec or a $ reference to it, otherwise npm stops with EOVERRIDE.
{
"dependencies": {
"hoek": "^4.2.1"
},
"overrides": {
"hoek": "$hoek"
}
}Verify
Every copy of the package in the tree now resolves to the pinned version.
npm ls hoek --allConfirmed by docs.npmjs.com
How do I stop it coming back?
- Add overrides and remove npm-force-resolutions in one commit, so two mechanisms never compete over the lockfile.
- Write the advisory or bug that justified each override into the pull request, and delete the entry once the parent package ships a release with the fix.
- Let CI fail when npm audit still reports the advisory an override was meant to close.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.