在 WSL Ubuntu 20.04 上安装 npm 后,我收到消息“/usr/bin/env: ‘bash\r’: No such file or directory”

After installing npm on WSL Ubuntu 20.04 I get the message "/usr/bin/env: ‘bash\r’: No such file or directory"

当 运行 来自终端的 npm installnpm 命令时,我看到以下消息。执行 node 按预期工作。

    > npm install
    /usr/bin/env: ‘bash\r’: No such file or directory

这是一个行尾问题,但不是来自 Ubuntu。使用以下命令确保 nodenpm 已正确安装:

sudo apt install nodejs npm

然后通过在 WSL 中编辑 /etc/wsl.conf 文件来停止 Windows 路径变量与 WSL 共享。如果文件不存在,先执行sudo touch /etc/wsl.conf。使用命令sudo nano /etc/wsl.conf编辑文件,添加如下配置:

[interop]
appendWindowsPath = false

然后在Windows中使用命令wsl --shutdown重启WSL2。

注意:这也会影响 pyenv 命令,请参阅 /usr/bin/env: ‘bash\r’: No such file or directory: Incompatible line-endings (WSL?)

我认为更好的解决方案是在 WSL 上使用 nvm 安装 NOde,这样你就不会遇到这个问题。

Installing Node using nvm for Ubuntu 20.04

今天我通过 nvm 更新了 node 和 npm 后发生了这种情况。我意识到 echo $PATH 确实缺少 .nvm 路径。然后我注意到 npm use v16.14.0 会 re-add 它到 $PATH 并使 npm i 再次工作。但是,这只适用于当前选项卡并使其在新的 sessions/tabs 中工作,我必须 运行:

nvm alias default v16.14.0

我遇到了同样的问题,在安装 nodejs 和 npm 后,只需重新启动我的 wsl wsl --shutdown 解决了问题,你可以试试这个。

Nodejs已经安装在WSL外,所以我在WSLwindow里敲了npm命令,居然在Windows上调用了npm。 Windows和WSL节点都卸载后,我在WSL中重新安装了NODE就没有问题了

这是一个使用 NVM 的解决方案,NVM 是节点框架的版本管理器。建议的解决方案:

在 WSL 中安装 curl 以便从 Internet 下载:

sudo apt-get install curl

接下来获取 NVM 下载器脚本并 运行 它 bash:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

接下来,安装 NVM 的长期支持版本:

nvm install --lts

最后,安装节点:

nvm install node

您可以在 WSL documentation

中找到有关 NVM 的更多信息