node icon indicating copy to clipboard operation
node copied to clipboard

`C:\Program Files\nodejs\npm` command not found - suddenly stops for IDE and Windows Terminal, but not WSL Bash.exe. (on same system)

Open iPoetDev opened this issue 5 months ago • 7 comments

Version

18.15, 20.10

Platform

Microsoft Windows NT 11.0.22621.0 x64

Subsystem

NPM / NPM.cmd

What steps will reproduce the bug?

Yesterday: npm install works and packages installed. (Environmental variables as-is) Today: Running npm install generates Unknown command: "C:\Program Files\nodejs\npm" unexpectedly. System change: ordering of environmental variables for Git/bin/bash v system32/bash.exe (unrelated) for another project (since removed in further troubleshooting).

Step 1: Attempt 1: Simple attempt

  • I uninstalled NodeJs LTS v18 and upgraded to NodeJS and rebooted
  • Used BulkCrapUninstaller to uninstall/remove traces from registry.

Step 2: Attempt 2: Clean Un-/Re- install: Git, Node LTS v20

  • Removed Git 2.43 and Nodev20
  • Scanned the registry for any npm or node references
  • Removed all references in sysdm.cpl (runas:/ Admin) for Environmental Variables Then:
  • Rebooted
  • Installed Git, then NodeJs under Administrator runas/ elevated priviledges.
  • Ran the commands as below.

Configured the following

  • Git: c:\Program Files\git
  • Node: c:\Program Files\nodejs
    • AppData : c:\AppData\Roaming\npm
    • Manually copied the c:\Program Files\nodejs\node_modules\npm ==> c:\AppData\Roaming\npm
  • System Environments Variables and User Environmental Variables (see below)

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

node -v generates a version output to stdout: v20.10.0 npm -v generates a version output to stdout: v10.2.3

  • but on Windows npm help => Unknown command: "C:\Program Files\nodejs\npm" npm install => Unknown command: "C:\Program Files\nodejs\npm" or any oither
  • but in WSL2 (Ubuntu): C:\Windows\System32\Bash.exe npm help => works StdOut npm install => works StdOut

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

npm commands should execute in Windows 11 from within Windows Terminal (Powershellv7)

However, I have WSL installed and from C:\Windows\System32\bash.exe it runs as expected. WSL was installed over 7 days ago and npm ran as expected and is running as expected..

I use Jetbrains IDEs and use their Run Configurations heavily and these IDE rely on C:\Program Files\nodejs\npm.cmd. (Actually I found out this issue happen that previously running scripts stopped running in this interface). I rely on these IDE Run Configs for current projects.

The script npm in C:\Program Files\nodejs is not being detected.

What do you see instead?

Running npm install or npm help generates Unknown command: "C:\Program Files\nodejs\npm"

  • Irrespective of Reinstalling NodeJs several times.
  • Configuring the Environmental Vars and
    • Ordered the nodejs and npm to top of paths
    • Added additional USER custom vars
      • NODE_PATH: directory level
        • C:\Program Files\nodejs
      • NODE_EXE: file level
        • C:\Program Files\nodejs\node.exe
      • NPM_PATH: directory level
        • C:\Program Files\nodejs
        • C:\Program Files\nodejs\node_modules
        • C:\Program Files\nodejs\node_modules\npm\bin
      • NODE_EXE: files level
        • C:\Program Files\nodejs\npm.cmd
        • C:\Program Files\nodejs\npm
        • C:\Program Files\nodejs\npx
        • C:\Program Files\nodejs\npx.cmd

Exceptionally by re-ordering (a failed smoke test)

Path=C:\Program Files\nodejs\node_modules\npm\bin;
C:\Program Files\nodejs\;
C:\Users\`$User`\AppData\Roaming\npm;

Generates : such suggest that the node/npm is detecting the package at some entry point but refuses to use the npm script..

npm help
node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'C:\Program Files (x86)\Nodist\bin\node_modules\npm\bin\npm-cli.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v20.10.0
  • Windows Compatibility troubleshooter.
  • Autopatchng my user/system installed apps using WinGetUI 2.1.2

I have spent 4 to 5 hours trying to fix.

Additional information

IDE:

  • Webstorm: 2023.2.3
  • PyCharm: 2023.2.3

Environmental Variables $User is replacer for actual user path. Line breaks added for readability but remove for actual. System Path:

Path=C:\Program Files\nodejs\;
C:\Program Files\nodejs\node_modules\npm\bin;
C:\Users\`$User`\AppData\Roaming\npm;
C:\Program Files\Git\cmd;
C:\Python312\;
C:\Users\`$User`\scoop\shims;
C:\ProgramData\chocolatey\bin;
C:\Python312\Scripts\;C:\WINDOWS;
C:\WINDOWS\System32\OpenSSH\;
C:\WINDOWS\system32;
C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links;
C:\Program Files\Microsoft\Web Platform Installer\;
C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;
C:\Program Files (x86)\Nodist\bin;

User Path:

Path=C:\Users\`$User`\AppData\Roaming\npm;
C:\Users\`$User`\scoop\shims;
C:\Users\`$User`\AppData\Local\Microsoft\WindowsApps;
C:\Users\`$User`\AppData\Local\JetBrains\Toolbox\scripts;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links;
C:\Users\`$User`\AppData\Local\Programs\WingetUI\choco-cli\lib;
C:\Users\`$User`\AppData\Local\Programs\WingetUI\choco-cli\lib\make\tools\install\bin;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links\wget.exe;
C:\Python312;

MS Security Definitions Updates

- Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.403.260.0) - Current Channel (Broad): ): Installed 10/12/23
- Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.403.228.0) - Current Channel (Broad): Installed 09/12/23

Can provide a Belarc Advisory Inventory by private means.

iPoetDev avatar Dec 10 '23 20:12 iPoetDev

Update: Further testing under WindowsTerminal + conhost's cmd.exe and npm install works when

Path=C:\Program Files\nodejs\node_modules\npm\bin;
C:\Program Files\nodejs\;
C:\Users\`$User`\AppData\Roaming\npm;

But the behaviour inconsistent AS same command fails in Windows Terminal/Powershell

Further testing: in IDE for further behaviours on going.Need IDE to behave before issue and run configs to behave as expected.

In Webstorm:

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

npm install
Could not determine Node.js install directory
PS D:\Code\CodeInstitute\examples\AlpineTicTacToe>  

iPoetDev avatar Dec 10 '23 20:12 iPoetDev

And Node is accessing the UAC mechanism when IDE runs (under runas/ Administrator?). which send Webstorm into a UAC loop.

iPoetDev avatar Dec 10 '23 20:12 iPoetDev

Similar, but not the same profile https://github.com/nodejs/node/issues/29287

iPoetDev avatar Dec 11 '23 12:12 iPoetDev

Would using docker on Windows be a workaround verses using Windows as the native host/Dev container for npm globally with WSL co installed? I think the WSL Node/NPM install is hijacking the Windows node/npm install?

I think, given that I did on first run/execution the WSL Bash.exe (Windows\System32\Bash.exe) from a Window context before this npm concern, there is a side effect of WSL version of node/nom on Windows hijacking the priority execution of the Windows node/npm from the Window kernel over the WSL kernel on Windows.

Now npm runs inside the WSL Bash.exe and not from any other window's shell where command is not found.

iPoetDev avatar Dec 12 '23 15:12 iPoetDev

cc @nodejs/npm

marco-ippolito avatar Dec 15 '23 15:12 marco-ippolito

Version

18.15, 20.10

Platform

Microsoft Windows NT 11.0.22621.0 x64

Subsystem

NPM / NPM.cmd

What steps will reproduce the bug?

Yesterday: npm install works and packages installed. (Environmental variables as-is) Today: Running npm install generates Unknown command: "C:\Program Files\nodejs\npm" unexpectedly. System change: ordering of environmental variables for Git/bin/bash v system32/bash.exe (unrelated) for another project (since removed in further troubleshooting).

Step 1: Attempt 1: Simple attempt

  • I uninstalled NodeJs LTS v18 and upgraded to NodeJS and rebooted
  • Used BulkCrapUninstaller to uninstall/remove traces from registry.

Step 2: Attempt 2: Clean Un-/Re- install: Git, Node LTS v20

  • Removed Git 2.43 and Nodev20
  • Scanned the registry for any npm or node references
  • Removed all references in sysdm.cpl (runas:/ Admin) for Environmental Variables Then:
  • Rebooted
  • Installed Git, then NodeJs under Administrator runas/ elevated priviledges.
  • Ran the commands as below.

Configured the following

  • Git: c:\Program Files\git

  • Node: c:\Program Files\nodejs

    • AppData : c:\AppData\Roaming\npm
    • Manually copied the c:\Program Files\nodejs\node_modules\npm ==> c:\AppData\Roaming\npm
  • System Environments Variables and User Environmental Variables (see below)

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

node -v generates a version output to stdout: v20.10.0 npm -v generates a version output to stdout: v10.2.3

  • but on Windows npm help => Unknown command: "C:\Program Files\nodejs\npm" npm install => Unknown command: "C:\Program Files\nodejs\npm" or any oither
  • but in WSL2 (Ubuntu): C:\Windows\System32\Bash.exe npm help => works StdOut npm install => works StdOut

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

npm commands should execute in Windows 11 from within Windows Terminal (Powershellv7)

However, I have WSL installed and from C:\Windows\System32\bash.exe it runs as expected. WSL was installed over 7 days ago and npm ran as expected and is running as expected..

I use Jetbrains IDEs and use their Run Configurations heavily and these IDE rely on C:\Program Files\nodejs\npm.cmd. (Actually I found out this issue happen that previously running scripts stopped running in this interface). I rely on these IDE Run Configs for current projects.

The script npm in C:\Program Files\nodejs is not being detected.

What do you see instead?

Running npm install or npm help generates Unknown command: "C:\Program Files\nodejs\npm"

  • Irrespective of Reinstalling NodeJs several times.

  • Configuring the Environmental Vars and

    • Ordered the nodejs and npm to top of paths

    • Added additional USER custom vars

      • NODE_PATH: directory level

        • C:\Program Files\nodejs
      • NODE_EXE: file level

        • C:\Program Files\nodejs\node.exe
      • NPM_PATH: directory level

        • C:\Program Files\nodejs
        • C:\Program Files\nodejs\node_modules
        • C:\Program Files\nodejs\node_modules\npm\bin
      • NODE_EXE: files level

        • C:\Program Files\nodejs\npm.cmd
        • C:\Program Files\nodejs\npm
        • C:\Program Files\nodejs\npx
        • C:\Program Files\nodejs\npx.cmd

Exceptionally by re-ordering (a failed smoke test)

Path=C:\Program Files\nodejs\node_modules\npm\bin;
C:\Program Files\nodejs\;
C:\Users\`$User`\AppData\Roaming\npm;

Generates : such suggest that the node/npm is detecting the package at some entry point but refuses to use the npm script..

npm help
node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module 'C:\Program Files (x86)\Nodist\bin\node_modules\npm\bin\npm-cli.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
    at node:internal/main/run_main_module:28:49 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v20.10.0
  • Windows Compatibility troubleshooter.
  • Autopatchng my user/system installed apps using WinGetUI 2.1.2

I have spent 4 to 5 hours trying to fix.

Additional information

IDE:

  • Webstorm: 2023.2.3
  • PyCharm: 2023.2.3

Environmental Variables $User is replacer for actual user path. Line breaks added for readability but remove for actual. System Path:

Path=C:\Program Files\nodejs\;
C:\Program Files\nodejs\node_modules\npm\bin;
C:\Users\`$User`\AppData\Roaming\npm;
C:\Program Files\Git\cmd;
C:\Python312\;
C:\Users\`$User`\scoop\shims;
C:\ProgramData\chocolatey\bin;
C:\Python312\Scripts\;C:\WINDOWS;
C:\WINDOWS\System32\OpenSSH\;
C:\WINDOWS\system32;
C:\WINDOWS\System32\Wbem;
C:\WINDOWS\System32\WindowsPowerShell\v1.0\;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links;
C:\Program Files\Microsoft\Web Platform Installer\;
C:\Program Files\dotnet\;C:\Program Files\PowerShell\7\;
C:\Program Files (x86)\Nodist\bin;

User Path:

Path=C:\Users\`$User`\AppData\Roaming\npm;
C:\Users\`$User`\scoop\shims;
C:\Users\`$User`\AppData\Local\Microsoft\WindowsApps;
C:\Users\`$User`\AppData\Local\JetBrains\Toolbox\scripts;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links;
C:\Users\`$User`\AppData\Local\Programs\WingetUI\choco-cli\lib;
C:\Users\`$User`\AppData\Local\Programs\WingetUI\choco-cli\lib\make\tools\install\bin;
C:\Users\`$User`\AppData\Local\Microsoft\WinGet\Links\wget.exe;
C:\Python312;

MS Security Definitions Updates

- Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.403.260.0) - Current Channel (Broad): ): Installed 10/12/23
- Security Intelligence Update for Microsoft Defender Antivirus - KB2267602 (Version 1.403.228.0) - Current Channel (Broad): Installed 09/12/23

Can provide a Belarc Advisory Inventory by private means.

I am having the exact same issue How did you solve yours please

devisreal avatar Dec 27 '23 01:12 devisreal

Did you install Node.js on WSL (https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl)? This resolved the same issue for me.

CAMA5163 avatar Feb 13 '24 08:02 CAMA5163