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

### Version

18.15, 20.10

### Platform

Microsoft Windows NT 11.0.22621.0 x64

### Subsystem

NPM / NPM.cmd

### What steps will reproduce the bug?

**<ins>Yesterday</ins>**: `npm install` works and packages installed. (Environmental variables as-is)
**<ins>Today</ins>**: 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)
```cmd
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..
```cmd
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.