Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm on Windows fails with module not found for Node.js 22.0.0 #52682

Closed
prabhu opened this issue Apr 25, 2024 · 47 comments
Closed

npm on Windows fails with module not found for Node.js 22.0.0 #52682

prabhu opened this issue Apr 25, 2024 · 47 comments
Labels
confirmed-bug Issues with confirmed bugs. npm Issues and PRs related to the npm client dependency or the npm registry. windows Issues and PRs related to the Windows platform.

Comments

@prabhu
Copy link

prabhu commented Apr 25, 2024

Version

22

Platform

Windows

Subsystem

No response

What steps will reproduce the bug?

npm install command is enough to reproduce the issue.

Example workflow: https://github.com/CycloneDX/cdxgen/actions/runs/8823813113/job/24236408405#step:10:18

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?

No response

What do you see instead?

node:internal/modules/cjs/loader:1205
  throw err;
  ^

Error: Cannot find module 'C:\npm\prefix\node_modules\npm\bin\npm-cli.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1202:15)
    at Module._load (node:internal/modules/cjs/loader:1027:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:187:14)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Additional information

No response

@Thanaen
Copy link

Thanaen commented Apr 25, 2024

For your information, I have the same problem on my Windows machine, so it's not just a problem with Github actions

PS C:\Users\me> node --version
v22.0.0
PS C:\Users\me> npm --version
node:internal/modules/cjs/loader:1205
  throw err;
  ^

Error: Cannot find module 'C:\Users\me\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1202:15)
    at Module._load (node:internal/modules/cjs/loader:1027:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:187:14)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

@danieljameswilliams
Copy link

I am also experiencing this.

@targos
Copy link
Member

targos commented Apr 25, 2024

@Thanaen

  • How did you install/update Node.js?
  • Does C:\Users\me\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js exist on your system?

I'm currently not able to reproduce on my Windows machine. I tried:

  • With Volta version manager (my usual setup)
  • With the offitial .msi installer (in a Windows Sandbox)
C:\Users\WDAGUtilityAccount>node -v
v22.0.0

C:\Users\WDAGUtilityAccount>npm -v
10.5.1

@Thanaen
Copy link

Thanaen commented Apr 25, 2024

@targos
I updated Node via the node-v22.0.0-x64 installer, downloaded directly from the Node website.

When I saw that it didn't work, I uninstalled Node, then did a clean install, with the same installer file. (after cleaning up all the cache files etc)

I then tried activating corepack and installing npm via corepack (corepack enable then corepack prepare npm@latest --activate)

After checking, it seems that the C:\Users\me\AppData\Roaming\npm folder doesn't exist 😅

Also, I've changed the npm_config_cache environment variable to point to my Dev Drive, so maybe that's a factor?
PS: I've tried removing my environment variable, but it doesn't fix the problem

@targos targos added the windows Issues and PRs related to the Windows platform. label Apr 25, 2024
@Thanaen
Copy link

Thanaen commented Apr 25, 2024

@targos I've just tried installation via Volta, and npm works correctly.
Maybe a problem with the MSI installer and Windows 11?

@targos
Copy link
Member

targos commented Apr 25, 2024

I don't know how to reproduce on my Windows system but it seems indeed to be a general issue on GitHub actions (which download the binary from https://nodejs.org/dist/v22.0.0/node-v22.0.0-win-x64.7z): https://github.com/nodejs/citgm/actions/runs/8831148407/job/24245743480?pr=1056

@alvicode
Copy link

have the same issue

@targos
Copy link
Member

targos commented Apr 25, 2024

The main difference I see between v20/v21 and v22 is the presence of npm.ps1 and npx.ps1 in the distribution.
This is from #52009

/cc @nodejs/npm @lukekarrys

@targos targos added confirmed-bug Issues with confirmed bugs. npm Issues and PRs related to the npm client dependency or the npm registry. labels Apr 25, 2024
@targos
Copy link
Member

targos commented Apr 25, 2024

Also /cc @StefanStojanovic.

I'm trying to find a simple reproduction but I hit another problem. Here are the steps I took:

PowerShell 7.4.2
PS C:\Users\mzasso\Documents\node-v22.0.0-win-arm64\node-v22.0.0-win-arm64> .\node.exe -v
v22.0.0
PS C:\Users\mzasso\Documents\node-v22.0.0-win-arm64\node-v22.0.0-win-arm64> .\npm.cmd -v
10.5.1
PS C:\Users\mzasso\Documents\node-v22.0.0-win-arm64\node-v22.0.0-win-arm64> .\npm.ps1 -v
node.exe not found.

Suggestion [3,General]: The command "node.exe" was not found, but does exist in the current location.
PowerShell does not load commands from the current location by default (see ''Get-Help about_Command_Precedence'').

If you trust this command, run the following command instead:
PS C:\Users\mzasso\Documents\node-v22.0.0-win-arm64\node-v22.0.0-win-arm64>

@targos
Copy link
Member

targos commented Apr 25, 2024

Then I installed a global node.exe with Volta and hit another (still different, but similar) issue:

PS C:\Users\mzasso\Documents\node-v22.0.0-win-arm64\node-v22.0.0-win-arm64> .\npm.ps1 -v
node:internal/modules/cjs/loader:1205
  throw err;
  ^

Error: Cannot find module 'C:\Program Files\Volta\node_modules\npm\bin\npm-prefix.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1202:15)
    at Module._load (node:internal/modules/cjs/loader:1027:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:187:14)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v22.0.0
Could not determine Node.js install directory

Maybe the problem is that the npm.ps1 script tries to determine where npm is installed based on where the global node.exe is installed? This seems wrong.

@wraithgar
Copy link

This appears to be a problem w/ the base node installation in windows itself.

A related issue is npm/cli#6971

It seems that in some installations npm does not end up where it belongs? Or is missing directories?

@wraithgar
Copy link

wraithgar commented Apr 25, 2024

Maybe the problem is that the npm.ps1 script tries to determine where npm is installed based on where the global node.exe is installed?

No, it tries to ask windows where node.exe is

$nodebin = $(Get-Command $nodeexe -ErrorAction SilentlyContinue -ErrorVariable F).Source

It then looks to the parent folder of the node executable and starts looking for npm there

$nodedir = $(New-Object -ComObject Scripting.FileSystemObject).GetFile("$nodebin").ParentFolder.Path

That's why this is happening

Suggestion [3,General]: The command "node.exe" was not found, but does exist in the current location.
PowerShell does not load commands from the current location by default (see ''Get-Help about_Command_Precedence'')

There is no node executable that is in a valid path. Windows is refusing to consider the one in the cwd as valid when npm asks for it because of command precedence.

@wraithgar
Copy link

npm.cmd looks for node using %~dp0\node.exe. Is it getting a different answer for this than Get-Command?

@targos
Copy link
Member

targos commented Apr 25, 2024

npm.cmd looks for node using %~dp0\node.exe. Is it getting a different answer for this than Get-Command?

Yes. %~dp0 is the directory that contains npm.cmd

@wraithgar
Copy link

npm has to make an assumption that npm and node are in the same place, and based on that can go find itself. npm.cmd and corepack do this by evaluating %~dp0\node.exe. npm.ps1 does this by calling Get-Command node.exe.

Is there another node.exe that is in the path? How do these strings materially differ, shouldn't they be the same?

It looks like that difference is the root cause here. We'll keep this issue open to track the work instead of making duplicates.

While we're on the subject however, it appears that this npm.ps1 file is not properly digitally signed in the node installation. cf npm/cli#7280.

@wraithgar
Copy link

This may represent a larger issue that has to date gone unnoticed. What npm is trying to make sure of is that node and npm are both reached by the same method. If there is some other node installation that is called when a person runs node that differs than the one npm shipped with that could cause problems. There is no guarantee that other version of node even works with this version of npm.

If Get-Command node.exe is not returning the same node.exe that is alongside the file that runs when npm.ps1 is called, that is an issue. It seems like it's an issue w/ the installer in that it's not putting the version of node that was installed properly in the user's PATH.

@wraithgar
Copy link

In the short term, we'll update the .ps1 script to use %dp0, @lukekarrys is working on this.

@lukekarrys
Copy link
Member

npm.cmd looks for node using %~dp0\node.exe. Is it getting a different answer for this than Get-Command?

Yes. %~dp0 is the directory that contains npm.cmd

I think this is a mistake in the ps1 scripts that npm ships with then. They should be using %~dp0 instead of Get-Command. I'll make a PR to npm fixing this in npm.ps1 and npx.ps1.

It appears that this npm.ps1 file is not properly digitally signed in the node installation. cf npm/cli#7280.

I would be curious if anyone knows how to fix the issue with digitally signing the ps1 scripts. I don't think it's directly related to this issue, but v22 is the first time these scripts are included in the Node installer. I'm not sure if this is something that should be fixed on the npm or node side.

@richardlau
Copy link
Member

%~dp0 is a .cmd/batch file thing. Powershell equivalents (depending on the version being targetted): https://stackoverflow.com/a/36417541

@richardlau
Copy link
Member

As for signing, Node.js signs assets it produces (the node.exe file, and msi installer). The logic for that is in vcbuild.bat. However, I'd be a little wary of using Node.js' signing certificate to sign something originating from outside of Node.js.

@StoneCypher
Copy link

This is the expected error if the image just didn't set the path

@cclauss
Copy link
Contributor

cclauss commented May 2, 2024

https://github.com/actions/node-versions/releases/tag/22.1.0-8926142033 fixes things in GitHub Actions for me.

      - uses: actions/setup-node@v4
        with:
          node-version: 22.1

@targos targos changed the title GitHub action for Windows fails with module not found error for node 22 npm on Windows fails with module not found for Node.js 22.0.0 May 2, 2024
@jameslund
Copy link

jameslund commented May 3, 2024

This issue is easy to reproduce if one creates a windows docker container and happens to install nodejs via chocolatey choco install -g nodejs. If one opens a command prompt, npm works fine. Using powershell however, attempting to run npm fails with the error from the OP. The solution for my container is running from command prompt only npm install -g npm as pointed out by @cclauss .

@imlehongphuc
Copy link

imlehongphuc commented May 4, 2024

INSTALL THE LATEST VERSION v22.1.0 NODEJS , THAT WOULD FIX THE BUG GUYS ! I TRIED AND GOOD TO USE !

@lukekarrys
Copy link
Member

This issue has been fixed in the latest npm 10 and Node 22 releases.

I'm going to close this but open a new issue for something we found from debugging this:

The Windows installer places the npm shim files in a location that takes precedence over the global npm packages directory. So subsequent npm installs via npm install npm --global will install a new npm version, but won't update the shims placed by the Windows installer.

This is only an issue when there exists a bug in the shim files like in this issue. In that scenario installing a new version of npm won't fix the shim files, which feels like an oversight because those files live in the npm repository.

@mrgrain
Copy link

mrgrain commented May 8, 2024

This issue has resurfaced in Node.js 20.13.0 which was released earlier today.
See #52884

@timjbrown
Copy link

timjbrown commented May 9, 2024

I get this error in vscode in Powershell running any npm or npx command like:

npm -v
Error: Cannot find module 'C:\Users\NAME\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at Module._load (node:internal/modules/cjs/loader:986:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}

But running in Command Prompt works:

npm -v
10.5.2

Spent 8 hours debugging removing/reinstalling/fixing with installer, restarting vscode/computer, removing adding suggested path variables, etc before realizing the command worked in Command Prompt but not Powershell.

b-ma added a commit to ircam-ismm/node-web-audio-api that referenced this issue May 9, 2024
@cclauss
Copy link
Contributor

cclauss commented May 9, 2024

@timjbrown Your issue is fixed in the latest versions of Node.js and npm.

undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue May 9, 2024
This commit addresses the issue where Windows GitHub runners experience
failures due to unstable Node.js releases, particularly version 20.13.0,
as detailed in nodejs/node#52884 and nodejs/node#52682.

The 'check-latest' input in the 'setup-node' GitHub Action forces
every job to verify and potentially install the latest Node.js version.
This input was originally introduced to reduce maintenance efforts to
keep CI/CD setup up-to-date with the latest Node version.

However, the necessity to always run the latest Node.js version is not
critical for the CI/CD setup. Additionally, it causes increate network
requests and may inadvertently introduce unstable Node.js versions.

This commit removes the 'check-latest' option to prevent the immediate
adoption of new, potentially unstable Node.js releases, thus simplifying
the CI/CD pipeline. This keeps  CI/CD process is robust and predictable,
reducing the chances of unexpected disruptions in service deployment.
@marco-ippolito marco-ippolito unpinned this issue May 10, 2024
undergroundwires added a commit to undergroundwires/privacy.sexy that referenced this issue May 10, 2024
This commit addresses the issue where Windows GitHub runners experience
failures due to unstable Node.js releases, particularly version 20.13.0,
as detailed in nodejs/node#52884 and nodejs/node#52682.

The 'check-latest' input in the 'setup-node' GitHub Action forces
every job to verify and potentially install the latest Node.js version.
This input was originally introduced to reduce maintenance efforts to
keep CI/CD setup up-to-date with the latest Node version.

However, the necessity to always run the latest Node.js version is not
critical for the CI/CD setup. Additionally, it causes increased network
requests and may inadvertently introduce unstable Node.js versions.

This commit removes the 'check-latest' option to prevent the immediate
adoption of new, potentially unstable Node.js releases, thus simplifying
the CI/CD pipeline. This keeps  CI/CD process is robust and predictable,
reducing the chances of unexpected disruptions in service deployment.
elf-pavlik added a commit to elf-pavlik/CommunitySolidServer that referenced this issue May 15, 2024
joachimvh pushed a commit to CommunitySolidServer/CommunitySolidServer that referenced this issue May 15, 2024
* test: update CI node versions

* Update npm-test.yml

* revert changes to quotes

* 22.0 broken on windows

nodejs/node#52682
@ATreeShine
Copy link

There is a way to fix it. At least that's how I fixed it. Run CMD and put this command: npm i -g npm. It will take few seconds to install and then you will face this issue again.

@Rj979
Copy link

Rj979 commented Aug 10, 2024

PS C:\Users\Scratch> node bot.js
node:internal/modules/cjs/loader:1148
throw err;
^

Error: Cannot find module 'C:\Users\Scratch\bot.js'
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at Module._load (node:internal/modules/cjs/loader:986:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
at node:internal/main/run_main_module:28:49 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}

Node.js v20.16.0

any way to fix this??

@Ice-Citron
Copy link

@Rj979 Same. same version worked on my mac, but not windows. I tried installing v22, v18, v20 with Volta and msi and choco. All didn't work. Wasted 3 hrs. Nice...

I should've gave up and stuck to mac instead...

@Ice-Citron
Copy link

@Rj979 Nevermind. I fixed it. And lesson learnt for me. Because I used a "&" in my project path and it wasn't able to start properly.

Try and properly uninstall version 20.16.0. Alongside with its npm file. And I reinstalled version 22.6.0 using the msi file. And it worked!

Note: sorry for doubting node js, And i learnt a lesson to never used "&" in windows again

@nicetomytyuk
Copy link

I'm having the same issue:

node:internal/modules/cjs/loader:1148
  throw err;
  ^

Error: Cannot find module 'C:\Users\igor.mytyuk\Desktop\Projects\visualposmobile\node_modules\npm\bin\npm-prefix.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v20.16.0
node:internal/modules/cjs/loader:1148
  throw err;
  ^

Error: Cannot find module 'C:\Users\igor.mytyuk\Desktop\Projects\visualposmobile\node_modules\npm\bin\npm-cli.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Module._load (node:internal/modules/cjs/loader:986:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:174:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v20.16.0
Command "npm.cmd" failed with exit code 1

The node -v and npm -v commands does work and returns:

PowerShell 7.4.5
PS C:\Users\igor.mytyuk> node -v
v20.16.0
PS C:\Users\igor.mytyuk> npm -v
10.8.2
PS C:\Users\igor.mytyuk>

The node has been installed from the official website.

To resolve the issue I had to rerun the node installation via Chocolatey.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. npm Issues and PRs related to the npm client dependency or the npm registry. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests