I can't use the npm command on Windows 10 with WSL2 ON (ubuntu terminal)

Viewed 2996

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:~#
1 Answers

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.

Related