NthDegree
NthDegree

Reputation: 1341

npm-force-resolutions not working when installing a new package

I'm using the scripts section of the package.json to force resolutions:

"preinstall": "npx npm-force-resolutions"

in the resolutions section, I have entered graceful-fs with a specified version:

"resolutions": {
  "graceful-fs": "^4.2.4",
},

When i run npm i everything is installed correctly, the set versions are taken in to account. But later on when I install an additional module, e.g. npm i random-package, my set versions are being thrown away and I endup with [email protected] and other low versions in some dependencies.

If I clear the node_modules folder and run npm i again, everything is alright again.

I also tried setting the resolution more specific, like

"resolutions": {
  "glob/**/graceful-fs": "^4.2.4",
},

but this doesn't help.

I also tried:

but no luck.

what am I missing?

Upvotes: 30

Views: 66203

Answers (5)

R. Oosterholt
R. Oosterholt

Reputation: 8080

Best way is to change the preinstall script to this:

"preinstall": "([ ! -f package-lock.json ] && npm install --package-lock-only --ignore-scripts --no-audit); npx npm-force-resolutions"

This will only run npm install to create your initial package-lock.json when it does not exist yet.
This is much faster than always running both (npm + npx).

As of npm 8.3.0, you can also use npm's override:

{
  "overrides": {
    "graceful-fs": "^4.2.4"
  }
}

Upvotes: 16

Jonathan V.n.
Jonathan V.n.

Reputation: 43

If all of the above answers don't work and you still get sh: npm-force-resolutions: command not found try the following:

Just change:

"preinstall": "npx npm-force-resolutions"

To:

"preinstall": "npx force-resolutions"

npx force-resolutions does not run when no package-lock.json is detected, and allows the next command inline to be executed as normal

Credit to: https://github.com/rogeriochaves/npm-force-resolutions/issues/10#issuecomment-885458937

Upvotes: 4

Fabrice Marquès
Fabrice Marquès

Reputation: 81

in the resolutions section, you must fix version

"resolutions": {
  "graceful-fs": "4.2.4",
},

Upvotes: 8

Kamil Sobczyk
Kamil Sobczyk

Reputation: 387

The best solution for me to automate this was modifying preinstall script as above:

"preinstall": "npm install --package-lock-only --ignore-scripts && npx npm-force-resolutions",

Upvotes: 21

Mudasar Rauf
Mudasar Rauf

Reputation: 552

Hi @NthDegree the only way which worked for me was to first run the normal npm install and then add the packages-lock.json file to git. After doing that when you add "preinstall": "npx npm-force-resolutions", it always updates the dependency resolution to the version mentioned.

I am not sure if adding packages-lock.json file to git is good or bad but by using this method the CI/CD pipeline works as well.

Upvotes: 4

Related Questions