cli icon indicating copy to clipboard operation
cli copied to clipboard

fix: Fix npm install --prefix on windows

Open nipunn1313 opened this issue 1 year ago • 2 comments

I found that globalDir/localDir and globalPrefix/localPrefix end up being the same value when --prefix is set. This was surprising to me, but it does appear that globalPrefix is set to match prefix at the end of loading.

This means that the globalTop logic is actually incorrect here. It accidentally worked on non-windows machines.

For example if running npm i --prefix scripts

On non-windows this.npm.globalDir = $CWD/scripts/lib/node_modules

On windows this.npm.globalDir = $CWD/scripts/node_modules

IMHO, the logic inside of npmconfig is also incorrect here, since globalDir is being set to this bogus $CWD/scripts/lib/node_modules

However, this fix should work regardless - since it stops relying on the specific values of globalDir.

I manually tested npm install and npm install --prefix on both windows and mac. Everything works now.

References

Fixes #7722

nipunn1313 avatar Aug 15 '24 09:08 nipunn1313

This needs tests

wraithgar avatar Aug 16 '24 13:08 wraithgar

good idea. I'll see when I can make some time for it. install.test.js has some examples which I had taken a quick pass over. It was a little hard to figure out how to get the equivalent of --prefix to happen since the tests don't take command line args directly. I'll have to dive a bit to figure out how to get coverage.

If anyone out there reading this has time to offer help, I'd take it.

nipunn1313 avatar Aug 16 '24 17:08 nipunn1313

Closed by https://github.com/npm/cli/pull/8269

mbtools avatar May 13 '25 10:05 mbtools