node icon indicating copy to clipboard operation
node copied to clipboard

`npm` is installed wrongly on windows machine

Open anonrig opened this issue 1 year ago • 24 comments

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

anonrig avatar Jun 21 '24 20:06 anonrig

Maybe related to https://github.com/npm/cli/issues/7089?

richardlau avatar Jun 21 '24 20:06 richardlau

cc @nodejs/npm this impacts all windows npm users.

anonrig avatar Jun 21 '24 21:06 anonrig

Also we used to create AppData\Roaming\npm in the msi installer, but stopped doing so: https://github.com/nodejs/node/commit/0ae8bf8dbc3d5db9a604c7be66aa22893e109fa0

richardlau avatar Jun 21 '24 21:06 richardlau

@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

abrowncownow avatar Jun 21 '24 21:06 abrowncownow

@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

anonrig avatar Jun 21 '24 22:06 anonrig

Does npm i -g npm also fail or does it perhaps resolve the issue?

tniessen avatar Jun 22 '24 10:06 tniessen

Does npm i -g npm also fail or does it perhaps resolve the issue?

It might but running it would destroy the repro in case someone needs it?

anonrig avatar Jun 23 '24 00:06 anonrig

@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

lukekarrys avatar Jun 25 '24 21:06 lukekarrys

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.

huseyinacacak-janea avatar Jun 26 '24 11:06 huseyinacacak-janea

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 avatar Jun 29 '24 00:06 anonrig

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

anonrig avatar Jun 29 '24 00:06 anonrig

@MylesBorins I think this requires your attention

anonrig avatar Jun 29 '24 00:06 anonrig

@nodejs/npm

GeoffreyBooth avatar Jun 29 '24 00:06 GeoffreyBooth

@anonrig why did you tag this tsc-agenda? What benefit would this get from a 20 person meeting?

benjamingr avatar Jun 29 '24 08:06 benjamingr

Here are excerpts from some of my own comments from internal discussions that led up to 0ae8bf8dbc3d5db9a604c7be66aa22893e109fa0.

Creating AppData\Roaming\npm was 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 to perMachine. 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\npm somehow 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).

tniessen avatar Jun 29 '24 10:06 tniessen

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

joyeecheung avatar Jun 29 '24 10:06 joyeecheung

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

targos avatar Jun 29 '24 13:06 targos

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

ruyadorno avatar Jun 29 '24 16:06 ruyadorno

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

anonrig avatar Jun 29 '24 16:06 anonrig

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.

That's interesting because the npm docs say:

prefix Configuration

The prefix config 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/node rather than {prefix}/node.exe.

When the global flag 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_modules folder under the prefix. When installing locally, this means that you can require("packagename") to load its main module, or require("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, no lib folder.)

richardlau avatar Jun 29 '24 17:06 richardlau

@richardlau sounds like my assumptions might be wrong then 😅

ruyadorno avatar Jun 29 '24 17:06 ruyadorno

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.

tniessen avatar Jun 30 '24 00:06 tniessen

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.

anonrig avatar Jun 30 '24 00:06 anonrig

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

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

hzhang20902 avatar Jun 30 '24 00:06 hzhang20902

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.

StefanStojanovic avatar Jul 01 '24 10:07 StefanStojanovic

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.

tniessen avatar Jul 01 '24 12:07 tniessen

https://github.com/nodejs/help/issues/4436 seems to be reporting a similar issue, are they related?

avivkeller avatar Jul 05 '24 00:07 avivkeller

It has been 2 weeks, and there is still no solution available from NPM team. @nodejs/npm @nodejs/tsc

anonrig avatar Jul 05 '24 01:07 anonrig

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

hzhang20902 avatar Jul 05 '24 01:07 hzhang20902

Does npm.cmd work?

avivkeller avatar Jul 05 '24 01:07 avivkeller