[BUG] npm on windows cannot update itself because of system/user path env order
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 10.1k
- Forks
- 4.7k
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 19
Description
Is there an existing issue for this?
- I have searched the existing issues
This issue exists in the latest npm version
- I am using the latest npm
Current Behavior
C:\>npm i -g npm@9.2.0
C:\>npm -v
8.19.2
C:\>npm ls -g npm
C:\Users\hyrious\AppData\Roaming\npm
`-- npm@9.2.0
Node.js for Windows installs a builtin npm with node.exe, and then it sets 2 PATH variables:
- System:
C:\Program Files\nodejs(1) - User:
%appdata%\npm(2)
You can see the builtin npm.cmd exists with node.exe in the install dir:
C:\>dir /b "C:\Program Files\nodejs"
node.exe
...
npm.cmd
The problem is Windows sorts the system PATH before user's (1 before 2), resulting in where npm always choose the one in the install dir instead of %appdata%. In the mean time npm i -g npm installs the new npm in user's path.
C:\>where npm
C:\Program Files\nodejs\npm
C:\Program Files\nodejs\npm.cmd
C:\Users\hyrious\AppData\Roaming\npm\npm
C:\Users\hyrious\AppData\Roaming\npm\npm.cmd
C:\>path
PATH=[...system path];[...user path]
As a result the builtin npm never gets updated.
Expected Behavior
I'm not sure which is the best solution to handle this, but the expecting behavior is to let npm i -g npm success on Windows.
Workarounds:
-
Since npm expects %appdata% precedes install_dir, we can just move the entry from user's path variable to system's. i.e. System =
...;%appdata%\npm;C:\Program Files\nodejs. -
People may not like 1 where a user's path is put in system level. So on the other hand, it would be better if npm itself can know it was installed in program files and it just updates itself there. i.e. There will be no
%appdata%\npmany more.The downside is node may not have the permission to edit files in program files. You'd have to tell user to run
npm i -g npmwith administrator permission. -
An implementation of 2 could be that, npm-cli.js always check if %appdata%\npm exists then switch to that file instead of current one if itself was run from the install_dir.
Steps To Reproduce
- First of all, get a Windows
- Download the Windows installer from https://nodejs.org, I suggest using the LTS one because it has npm@8, which we could update to 9
- Run
npm i -g npm@9 - Run
npm -v, it is still 8 - Run
npm ls -g npm, it says npm@9 is installed in %appdata%
Environment
- npm: 8.19.2
- Node.js: 18.12.1
- OS Name: Windows 10 (22H2, 19045.2364)
- System Model Name: GL552JX
- npm config:
; "builtin" config from C:\Program Files\nodejs\node_modules\npm\npmrc
prefix = "C:\\Users\\hyrious\\AppData\\Roaming\\npm"
; "global" config from C:\Users\hyrious\AppData\Roaming\npm\etc\npmrc
; node bin location = C:\Program Files\nodejs\node.exe
; node version = v18.12.1
; npm local prefix = C:\Users\hyrious
; npm version = 8.19.2
; cwd = C:\Users\hyrious
; HOME = C:\Users\hyrious
; Run `npm config ls -l` to show all defaults.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Windows PATH ordering with the listed npm and Node.js versions, then inspect npm-cli.js and the Node installer configuration in product.wxs. Compare the installed global npm location with the executable selected by where npm; done means npm i -g npm on Windows selects and runs the updated npm rather than the bundled copy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- cli, operating-systems
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100