`npm` is installed wrongly on windows machine
I'm getting the following error when trying to run a simple npx command on Windows. I opened the issue to this repository because we are distributing npm, we define the location of npm in a Windows machine.
I reinstalled Node.js from nodejs.org with the latest v22, and I can not use npm at all. Any input/help is appreciated.
PS: I'm using PowerShell.
PS C:\Users\yagiz\Desktop\coding\node> npx node-benchmark-compare .\bench.txt
npm error code ENOENT
npm error syscall lstat
npm error path C:\Users\yagiz\AppData\Roaming\npm
npm error errno -4058
npm error enoent ENOENT: no such file or directory, lstat 'C:\Users\yagiz\AppData\Roaming\npm'
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.log
cc @nodejs/platform-windows
Maybe related to https://github.com/npm/cli/issues/7089?
cc @nodejs/npm this impacts all windows npm users.
Also we used to create AppData\Roaming\npm in the msi installer, but stopped doing so: https://github.com/nodejs/node/commit/0ae8bf8dbc3d5db9a604c7be66aa22893e109fa0
@anonrig Can you post the details of the log C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.log
@anonrig Can you post the details of the log
C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.log
0 verbose cli C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
1 info using [email protected]
2 info using [email protected]
3 silly config load:file:C:\Program Files\nodejs\node_modules\npm\npmrc
4 silly config load:file:C:\Users\yagiz\Desktop\coding\node\.npmrc
5 silly config load:file:C:\Users\yagiz\.npmrc
6 silly config load:file:C:\Users\yagiz\AppData\Roaming\npm\etc\npmrc
7 verbose title npm exec node-benchmark-compare .\bench.txt
8 verbose argv "exec" "--" "node-benchmark-compare" ".\\bench.txt"
9 verbose logfile logs-max:10 dir:C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-
10 verbose logfile C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.log
11 silly logfile start cleaning logs, removing 1 files
12 silly logfile done cleaning log files
13 silly packumentCache heap:4345298944 maxSize:1086324736 maxEntrySize:543162368
14 http fetch GET 200 https://registry.npmjs.org/node-benchmark-compare 152ms (cache revalidated)
15 silly packumentCache heap:4345298944 maxSize:1086324736 maxEntrySize:543162368
16 verbose stack Error: ENOENT: no such file or directory, lstat 'C:\Users\yagiz\AppData\Roaming\npm'
16 verbose stack at async lstat (node:internal/fs/promises:1026:18)
16 verbose stack at async #loadActual (C:\Program Files\nodejs\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\load-actual.js:112:20)
16 verbose stack at async exec (C:\Program Files\nodejs\node_modules\npm\node_modules\libnpmexec\lib\index.js:203:26)
16 verbose stack at async Npm.exec (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:207:9)
16 verbose stack at async module.exports (C:\Program Files\nodejs\node_modules\npm\lib\cli\entry.js:74:5)
17 error code ENOENT
18 error syscall lstat
19 error path C:\Users\yagiz\AppData\Roaming\npm
20 error errno -4058
21 error enoent ENOENT: no such file or directory, lstat 'C:\Users\yagiz\AppData\Roaming\npm'
22 error enoent This is related to npm not being able to find a file.
22 error enoent
23 verbose cwd C:\Users\yagiz\Desktop\coding\node
24 verbose os Windows_NT 10.0.22631
25 verbose node v22.3.0
26 verbose npm v10.8.1
27 verbose exit -4058
28 verbose code -4058
29 error A complete log of this run can be found in: C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.log
Does npm i -g npm also fail or does it perhaps resolve the issue?
Does
npm i -g npmalso fail or does it perhaps resolve the issue?
It might but running it would destroy the repro in case someone needs it?
@anonrig does it work if you use cmd instead of PowerShell? this is just to figure out if it is also a side effect of https://github.com/nodejs/node/issues/52682
I've looked into this issue and seen that this is caused by stopping creating AppData\Roaming\npm as @richardlau said. Since this is a security concern, it is unlikely that this commit will be reverted.
There are workarounds mentioned on the internet. Their basic idea is to create the appropriate folder, either manually by creating an empty folder or by installing a global module. The basic command to create this folder would be npm i -g npm, as this does not install an additional module, but moves the npm folder to global.
Additionally, you could install the module as an extra step before running npx node-benchmark-compare .\bench.txt. For example, in this case, you could run npm i -g node-benchmark-compare.
The npx documentation might be misleading for some users. It can be updated accordingly.
@lukekarrys, I tried from cmd and it is the same.
This might be a security issue, but regardless we should never ship a broken/buggy software. If this is indeed the default behavior, than I ask myself "why are we shipping a broken feature on Windows?"
cc @nodejs/tsc
@anonrig does it work if you use cmd instead of PowerShell? this is just to figure out if it is also a side effect of #52682
Didn't fix the issue.
@MylesBorins I think this requires your attention
@nodejs/npm
@anonrig why did you tag this tsc-agenda? What benefit would this get from a 20 person meeting?
Here are excerpts from some of my own comments from internal discussions that led up to 0ae8bf8dbc3d5db9a604c7be66aa22893e109fa0.
Creating
AppData\Roaming\npmwas an intentional addition in order to resolve https://github.com/nodejs/node-v0.x-archive/issues/8141, which appears to have been a common issue for users of npm. However, this was implemented before 4cfe5eb9af9d0a46ab6bfe3a4a49c4b1e43513b0, which changed the MSI installation scope toperMachine. There were concerns about creating the npm directory in that PR, albeit not related to security (see https://github.com/nodejs/node-v0.x-archive/pull/25640).
FWIW, I am not quite happy with this patch. My main concern is https://github.com/nodejs/node-v0.x-archive/issues/8141 becoming an issue again, but I am not sure under what circumstances it would. The only thing I can think of is if
AppData\Roaming\npmsomehow cannot be created by npm (i.e., by an unprivileged user), but then we probably also cannot create it from within the installer through an unprivileged action.
So while we weren't certain at the time, we did consider the possibility that on some Windows systems, this patch might re-introduce some old npm issues.
I still don't see a reasonable way for the Node.js installer to create that directory. What I'm wondering is if npm is assuming the directory exists (and it doesn't) or if npm would create the directory (but can't).
It seems like a UX issue that can be addressed with better docs and better suggestions in the errors. But it seems more appropriate to just switch to https://github.com/npm/cli/issues/7089
I don't see why npm couldn't create the directory when needed. It is in a path that belongs to the current user so permissions shouldn't be an issue
iirc node tweaks the npm prefix on windows in: tools/msvs/npm/npmrc
I believe that custom path does solve a few problems in windows but given that is not the default path that the standalone npm client is using it creates a split for windows users and can easily be a blind spot for the npm team to maintain.
If my assumptions above are not wrong, the fix would be for the npm cli team to clean up that prefix story on windows so that's straightforward to support it moving forward and of course start to create that missing folder (in case that's the one they want to standardize on).
@anonrig why did you tag this tsc-agenda? What benefit would this get from a 20 person meeting?
@benjamingr I understand you but you should know that: I've added the tsc-agenda because this is a major bug which is not addressed for more than a week on an operating system we have tier 1 support. The benefit of getting this to a 20 person meeting is to raise awareness, and expedite the fix of this problem, and potentially navigate to why no-one addressed this in a timely manner.
iirc node tweaks the npm prefix on windows in:
tools/msvs/npm/npmrcI believe that custom path does solve a few problems in windows but given that is not the default path that the standalone npm client is using it creates a split for windows users and can easily be a blind spot for the npm team to maintain.
That's interesting because the npm docs say:
prefix Configuration
The
prefixconfig defaults to the location where node is installed. On most systems, this is/usr/local. On Windows, it's%AppData%\npm. On Unix systems, it's one level up, since node is typically installed at{prefix}/bin/noderather than{prefix}/node.exe.When the
globalflag is set, npm installs things into this prefix. When it is not set, it uses the root of the current package, or the current working directory if not in a package already.Node Modules
Packages are dropped into the
node_modulesfolder under theprefix. When installing locally, this means that you canrequire("packagename")to load its main module, orrequire("packagename/lib/path/to/sub/module")to load other modules.Global installs on Unix systems go to
{prefix}/lib/node_modules. Global installs on Windows go to{prefix}/node_modules(that is, nolibfolder.)
@richardlau sounds like my assumptions might be wrong then 😅
Does this affect all Windows users or just a subset? I'm a bit surprised we haven't heard more complaints about this if it does affect all users.
Does this affect all Windows users or just a subset? I'm a bit surprised we haven't heard more complaints about this if it does affect all users.
I installed it through nodejs website and running on windows 11. I assume it is all users.
I'm getting the following error when trying to run a simple npx command on Windows. I opened the issue to this repository because we are distributing
npm, we define the location ofnpmin a Windows machine.I reinstalled Node.js from
nodejs.orgwith the latest v22, and I can not usenpmat all. Any input/help is appreciated.PS: I'm using PowerShell.
PS C:\Users\yagiz\Desktop\coding\node> npx node-benchmark-compare .\bench.txt npm error code ENOENT npm error syscall lstat npm error path C:\Users\yagiz\AppData\Roaming\npm npm error errno -4058 npm error enoent ENOENT: no such file or directory, lstat 'C:\Users\yagiz\AppData\Roaming\npm' npm error enoent This is related to npm not being able to find a file. npm error enoent npm error A complete log of this run can be found in: C:\Users\yagiz\AppData\Local\npm-cache\_logs\2024-06-21T20_29_57_155Z-debug-0.logcc @nodejs/platform-windows
This issue has happened a few times in recent years, and has been answered but not really solved.
npm i -g npm is the cleanest way to fix it
Does this affect all Windows users or just a subset?
This should only affect users that never installed Node.js while installer was making that folder. As I recall when uninstalling Node, that directory wasn't removed, so it'd stay there unless you manually delete it, which I doubt a lot of people did.
Creating the folder from the MSI is problematic for multiple reasons, not only because of security concerns. I don't think this is something we can cleanly fix inside the installer, except through some user instructions or best-effort approach.
https://github.com/nodejs/help/issues/4436 seems to be reporting a similar issue, are they related?
It has been 2 weeks, and there is still no solution available from NPM team. @nodejs/npm @nodejs/tsc
It has been 2 weeks, and there is still no solution available from NPM team. @nodejs/npm @nodejs/tsc
No it's been years, but if you feel so strongly about this, you can fix it yourself as it is open source and you can submit code for review. Otherwise, just gonna have to live with:
npm i -g npm
Does npm.cmd work?