0

I have node installed on my Windows 11 machine (on the Windows side).

I also have WSL2 setup and am trying to install the linux version of Node.js there.

After running the standard Ubuntu setup script for Node.js LTS:

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

node runs fine, but npm does not:

cameron@Nook:~$ node --version
v14.17.4
cameron@Nook:~$ npm --version
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
cameron@Nook:~$ which npm
/usr/bin/npm
cameron@Nook:~$ /usr/bin/npm --version
6.14.14
cameron@Nook:~$ which node
/usr/bin/node
cameron@Nook:~$ /usr/bin/node --version
v14.17.4
cameron@Nook:~$ whereis npm
npm: /usr/bin/npm /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd /mnt/c/Users/camer/AppData/Roaming/npm/npm /mnt/c/Users/camer/AppData/Roaming/npm/npm.cmd /mnt/c/Users/camer/AppData/Roaming/npm/npm.ps1

Curiously, which finds the expected version of npm but I have to run it directly to get it to work as expected. What is missing here?

I've seen solutions that suggest messing with the $PATH, but I don't see why that should be necessary.

Update: My PATH has linux paths listed first...

cameron@Nook:~$ tr ':' '\n' <<< $PATH
/usr/local/sbin
/usr/local/bin
/usr/sbin
/usr/bin
/sbin
/bin
/usr/games
/usr/local/games
/usr/lib/wsl/lib
/mnt/c/Program Files/Microsoft/jdk-16.0.1.9-hotspot/bin
/mnt/c/TwinCAT/Common64
/mnt/c/TwinCAT/Common32
/mnt/c/Python39/Scripts/
/mnt/c/Python39/
/mnt/c/Python38/Scripts/
/mnt/c/Python38/
/mnt/c/WINDOWS/system32
/mnt/c/WINDOWS
/mnt/c/WINDOWS/System32/Wbem
/mnt/c/WINDOWS/System32/WindowsPowerShell/v1.0/
/mnt/c/WINDOWS/System32/OpenSSH/
/mnt/c/ProgramData/chocolatey/bin
/mnt/c/Program Files (x86)/STMicroelectronics/STM32 ST-LINK Utility/ST-LINK Utility
/mnt/c/Program Files/Java/jdk1.8.0_211/bin
/mnt/c/Program Files (x86)/Yarn/bin/
/mnt/c/MinGW/msys/1.0/bin/
/mnt/c/Program Files (x86)/Wolfram Research/WolframScript/
/mnt/c/Program Files/Git/cmd
/mnt/c/Program Files/PuTTY/
/mnt/c/Program Files/nodejs/
/mnt/c/Users/camer/.windows-build-tools/python27/
/mnt/c/Users/camer/bin
/mnt/c/Users/camer/AppData/Local/Microsoft/WindowsApps
/mnt/c/Program Files (x86)/Nmap
/mnt/c/Users/camer/AppData/Local/Programs/Microsoft VS Code/bin
/mnt/c/Program Files (x86)/Avrdude
/mnt/c/Users/camer/AppData/Local/Yarn/bin
/mnt/c/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin
/mnt/c/Users/camer/AppData/Local/gitkraken/bin
/mnt/c/Program Files/GNU Octave/Octave-6.2.0/mingw64/bin
/mnt/c/Users/camer/AppData/Local/Microsoft/WindowsApps
/mnt/c/Users/camer/AppData/Roaming/npm
/snap/bin

Yes, I could probably clean some of these extra PATHs up but I don't see how this is affecting things. The linux PATHs are first...

I also do not have any weird aliases active:

cameron@Nook:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

PS, no, I will not be using nvm or n. They are anti-patterns, imho.

Cameron Tacklind
  • 5,764
  • 1
  • 36
  • 45
  • `/bin/sh^M`.... you have a line-ending problem. Linux expects NL, Windows uses CR+LF, one of your tools is getting badly confused by the difference. – Ben Voigt Aug 10 '21 at 20:07
  • Yes, that's what I'm asking. Why is bash on WSL finding the Program Files version of `npm`? – Cameron Tacklind Aug 10 '21 at 20:21
  • Any relevant shell aliases defined? – Ben Voigt Aug 10 '21 at 20:33
  • Windows is clearly exporting its `PATH` variable to Linux. I don't use Windows but remember that it also had a PATH environment variable (at least it did way back on Win2k). One quick fix is to add `/usr/bin` before the rest of the PATH in your shell start-up file (eg. `.bashrc` if you are using bash): `export PATH=/usr/bin:$PATH` – slebetman Aug 10 '21 at 21:44
  • The only reason I'm not leaving the above as an answer is because I don't have a Windows machine to test it on. If it works then I'll submit it as an answer. – slebetman Aug 10 '21 at 21:45
  • You can confirm that Windows exports its PATH to Linux by printing the `PATH` variable on Linux: `echo $PATH` – slebetman Aug 10 '21 at 21:54
  • I do use WSL, and @slebetman is correct -- WSL's `init` by default *appends* the Windows path to the Linux path. In most cases, the appending should make it so that your default `/usr/bin` still comes first, but please add the results of `echo $PATH` to your question. Also see [my answer here](https://stackoverflow.com/a/63458916/11810933) on the topic. – NotTheDr01ds Aug 10 '21 at 21:58
  • I've updated the question to include details about `$PATH` and `aliases` – Cameron Tacklind Aug 11 '21 at 00:10

0 Answers0