Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

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 show

Fix it fast

Most common cause, 1 of 2

The 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.

Fix
// package.json (root)
{
  "overrides": {
    "hoek": "4.2.1"
  }
}

npm pkg delete scripts.preinstall resolutions
npm install
Did this fix it?
Full steps for this cause: check, fix, verify

Short 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).

  • Applies to npm 11, npm 12.x, Node.js 22, 24 and 26
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
grep -n -A4 -E '"(resolutions|preinstall|overrides)"' package.json
npm install && npm ls hoek
# after your next npm install <name>:
npm ls hoek

Then find the line you see in this list and open its fix:

  1. 1The preinstall hook does not run when a package name is givennpm ls shows the forced version after a bare npm install, and the old one again right after npm install <name>
  2. 2The resolutions block has nothing that reads itpackage.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-package
  • package.json has "preinstall": "npx npm-force-resolutions" and a resolutions block, yet npm audit still flags the nested package
  • A 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.

Check
npm pkg get scripts.preinstall resolutions

Fix

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.

Fix
// package.json (root)
{
  "overrides": {
    "hoek": "4.2.1"
  }
}

npm pkg delete scripts.preinstall resolutions
npm install

Verify

The pin survives the next named install: after npm install <name>, npm ls still shows the forced version.

Verify
npm ls hoek --all

Confirmed by docs.npmjs.com

Did this fix it?

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.

Check
npm pkg get resolutions overrides

Fix

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.

Fix
{
  "dependencies": {
    "hoek": "^4.2.1"
  },
  "overrides": {
    "hoek": "$hoek"
  }
}

Verify

Every copy of the package in the tree now resolves to the pinned version.

Verify
npm ls hoek --all

Confirmed by docs.npmjs.com

Did this fix it?

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.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.