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

a 夏天 提交于 2021-01-29 18:18:52

问题


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.


回答1:


Try this

export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
sudo apt install npm



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/63716587/in-wsl2-ubuntu-20-04-for-windows-10-nodejs-is-installed-but-npm-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!