3

I'm trying to get the npm command to work but it doesn't want to. As i said in the title, I'm using the windows WSL2. I already have installed nodejs with Git Bash a long time ago but now I need it to work on my ubuntu terminal. How could I fix this issue?

My ubuntu terminal:

root@DESKTOP-U2RC2DU:~# node -v
v10.19.0
root@DESKTOP-U2RC2DU:~# npm
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory
root@DESKTOP-U2RC2DU:~#
Old Geezer
  • 14,854
  • 31
  • 111
  • 198
notsosmartdev
  • 33
  • 1
  • 3
  • I have the same issue. It is looking for npm in Windows (`/mnt/c/Program Files/nodejs` is your `C:\Program Files\nodejs`). `which npm` shows that `npm` is located in `/usr/bin`, and `/usr/bin` is on my PATH way above `/mnt/c/Program Files/nodejs`. I wonder why it is still looking in Windows. – Old Geezer Aug 04 '20 at 15:09

1 Answers1

8

Check your path in WSL. If the Windows version is found before the Linux version, it can cause problems.

There are a few possible workarounds for this that I know of:

  1. Create a startup script (e.g. bashrc or equivalent for your shell of choice) which removes the Windows node from path, or at least gives the Linux version higher precedence. Probably the simplest solution is to prepend any Linux version location to the path.

  2. Create a /etc/wsl.conf with ...

    [interop]
    appendWindowsPath = false
    

    This will remove all Windows paths from the WSL session. The downside is that some useful Windows utilities will no longer be in the WSL path, but you can always add them back in manually in your startup scripts. This is probably easier from a scripting perspective than removing paths manually, at least.

  3. If you don't need it anymore, and will be using the Linux/WSL version exclusively, you could uninstall the Windows nodejs.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70