Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WSL2: Ubuntu 20.04 for Windows 10 nodejs is installed but npm is not working

I am using WSL2: Ubuntu 20.04 in my Windows 10 operating system. I have installed nodejs using the command sudo apt-get install -y nodejs when I do node -v command I get v12.18.3

mrd@DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ node -v
v12.18.3

but when I do npm -v command I get this below command

mrd@DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ npm -v
-bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

I also do whereis command. Hope this will help to find solution.

mrd@DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ whereis node
node: /usr/bin/node /usr/include/node /mnt/c/Program Files/nodejs/node.exe /usr/share/man/man1/node.1.gz

mrd@DESKTOP-2EO5K4H:/mnt/c/Users/musfi$ whereis npm
npm: /usr/bin/npm /mnt/c/Program Files/nodejs/npm /mnt/c/Program Files/nodejs/npm.cmd /usr/share/man/man1/npm.1

I have tried almost all the stackoverflow solutions and github issues but nothing is worked for me.
Hope any kind soul has the solution to this problem. Thanks in advance.

like image 1000
Md. Musfiqur Rahaman Avatar asked Sep 03 '20 03:09

Md. Musfiqur Rahaman


People also ask

What to do when npm install is not working?

If your npm is broken: On Mac or Linux, reinstall npm. Windows: If you're on Windows and you have a broken installation, the easiest thing to do is to reinstall node from the official installer (see this note about installing the latest stable version).

How do I install npm files on Windows 10?

For Node. js projects, the easiest way to install npm packages is through the npm package installation window. To access this window, right-click the npm node in the project and select Install New npm Packages. In this window you can search for a package, specify options, and install.

Is Nodejs installed with npm?

NPM is included with Node. js installation. After you install Node. js, verify NPM installation by writing the following command in terminal or command prompt.


Video Answer


5 Answers

Try this

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sudo apt install npm
like image 149
Jatin Mehrotra Avatar answered Oct 21 '22 09:10

Jatin Mehrotra


Solution for following error: -bash: /mnt/c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

Edit ~/.bashrc Append at end of file:

# strip out problematic Windows %PATH%
PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g')

Now npm init will work.

like image 31
Subhasish Paul Avatar answered Oct 21 '22 10:10

Subhasish Paul


A better way is configuring /etc/wsl.conf in your Windows User directory.

Adding this into the /etc/wsl.conf, so Windows Path will not take the precedence

[interop]
appendWindowsPath=false

For more config details check the Microsoft Dev Blog here.

like image 6
Kidd Tang Avatar answered Oct 21 '22 09:10

Kidd Tang


To install nodejs in WSL don't use apt follow Microsoft's guidance:

  • https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl

See also how to remove nodejs if you installed it via apt:

  • https://askubuntu.com/questions/786015/how-to-remove-nodejs-from-ubuntu-16-04

For npm to work under WSL1:

  • You may also need to disable ipv6 (or configure to prefer ipv4) - if you are hitting these issues.
  • Under WSL1 I disabled ipv6 on my main NIC & npm install immediately began working.

WSL2 Notes:

NB: if you use a VPN your container connectivity may be broken under WSL2 (e.g with Cisco AnyConnect) - the fix works but may no longer be needed under AnyConnect (WSL2 on a VPN now works for me after a recent update @ end of July 2022)

I thought my WSL containers were running under WSL2 (I upgraded the WSL kernel with wsl --update) - while setting up Visual Studio with WSL I saw a WSL1 warning. You also have to upgrade containers:

wsl --set-version ubuntu-22.04 2
wsl --set-default-version 2

To get Visual Studio integration working properly with Ubuntu 22.04 in WSL you also currently have to upgrade gzip to install VS Code Server for x64 in WSL (code .: in the Linux terminal):

wget http://ftp.debian.org/debian/pool/main/g/gzip/gzip_1.12-1_amd64.deb
sudo dpkg -i ./gzip_1.12-1_amd64.deb

Finally I upgraded npm & everything works (choose one of the following commands):

  • nvm install-latest-npm
  • npm install -g npm@latest

Azure AD / CLI Notes

If you use nodejs with Azure Active Directory there seems to be an issue with the azure-cli forgetting credentials under WSL1 / WSL2 & persistently telling you to az login. In this case you need to run your local node development instances on Windows.

like image 4
Stuart Cardall Avatar answered Oct 21 '22 09:10

Stuart Cardall


For all Unix/Linux/MacOS operating systems, I would always rather go with the "Node Version Manager". It normally works flawlessly on Linux and MacOS (and there's a Windows port for it as well) and enables a very simple way of installing node and npm correctly without the need of being root.

See here: https://github.com/nvm-sh/nvm

I can confirm here on my machine that it also works on Ubuntu 20.04 on WSL2.

like image 1
donmartin Avatar answered Oct 21 '22 10:10

donmartin