无法运行NPM命令

时间:2016-09-03 20:41:25

标签: node.js npm

我一直在我的机器上使用NPM(Windows 10),但最近遇到了一个问题。我目前安装了Node.js,并在运行任何npm命令时收到以下错误。

  

问题:导致此错误的原因是解决问题的最佳方式。

命令:

$ npm install

输出/错误:

bash: /c/Program Files/nodejs/npm: /bin/sh^M: bad interpreter: No such file or directory

6 个答案:

答案 0 :(得分:12)

不是我的解决方案,但这似乎对我有用。似乎在使用WSL2时在$PATH中使用Windows文件夹结构会导致该解析错误,但是我不确定为什么。

  1. 转到您的用户根(cd ~
  2. 在您选择的编辑器(vi,nano等)中打开.bashrc
  3. 附加到文件末尾:PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g') # strip out problematic Windows %PATH%
  4. 关闭并重新打开所有终端窗口

来源:https://hackmd.io/@badging/wsl2#Troubleshooting-PATH

答案 1 :(得分:6)

使用nvm安装nodejs/npm,不会和windows的冲突。 npm 的路径变为(使用 nvm 安装后)/home/ubuntu/.nvm/versions/node/v14.16.0/bin/npm

Read More about setting up your Node.js development environment with WSL 2

答案 2 :(得分:0)

我正在使用Git Bash进行cli

我的情况是我的防病毒软件隔离了C:\Program Files\Git\user\bin\sh.exe文件,这就是$ npm init无法正常工作并显示此错误的原因:

  

bash:/ c / Program Files / nodejs / npm:/ bin / sh ^ M:错误的解释器:没有这样的文件或目录

当我还原该文件时,它开始正常工作。

答案 3 :(得分:0)

在我的 wsl2 上遇到过这个问题

不是修复而是解决方法。卸载c盘解决问题。

umount /mnt/c

答案 4 :(得分:0)

刚刚遇到同样的问题,发生此问题是因为 npm 安装在您的 Windows 机器上,而不是您的 WSL 机器上。你只需要在你的 linux 机器上安装 npm 然后它就会从 linux 而不是 windows 读取二进制文件,以防你想在你的 WSL 上使用 windows 路径。否则,如果您不需要 Windows 路径,您可以使用亚当提到的内容,将其添加到您的路径中:auto filepath1 = "/path1/file.txt"; auto filepath2 = "/path2/file.txt"; // We assume file_descriptor has been declared earlier for (const auto fpath: {filepath1, filepath2}) { file_descriptor = open(fpath) if (file_descriptor.open()) break; else printf("file %s did not work\n", fpath); } if (!file_descriptor.open()) return false; // or throw read_file(file_descriptor); return true; 然后使用 PATH=$(echo "$PATH" | sed -e 's/:\/mnt.*//g')

刷新外壳

答案 5 :(得分:0)

我使用了这些命令。

sudo apt update && sudo apt install curl -y

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash 

source ~/.profile 

nvm ls-remote 

nvm install v15 

node --version