sudo: npm: 命令未找到。

367

我想升级到最新版本的node。我正在按照http://davidwalsh.name/upgrade-nodejs上的说明进行操作。

但是当我执行以下操作时:

sudo npm install -g n

我遇到了以下错误:

sudo: npm: command not found

npm无需使用sudo。当我执行以下操作时:

whereis node

我看到:

node: /usr/bin/node /usr/lib/node /usr/bin/X11/node /usr/local/node

运行中:

which npm

显示:

/usr/local/node/bin/npm

我尝试了https://dev59.com/hG445IYBdhLWcg3wOnrW#5062718上的解决方案,但是我仍然遇到同样的错误。我还查看了/etc/sudoers文件,相关行是:

Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

我认为这看起来很好。我该如何使用sudo命令使NPM工作?


6
这个命令可以吗?sudo /usr/bin/npm install -g n - robertklep
2
你可以尝试重新安装Node吗?听起来好像你删除了npm。 - Jeff Sloyer
1
你可能已经将/usr/local/node/bin添加到了 $PATH 中,但是 npm 应该安装在 /usr/local/bin中。第一个目录不在 secure_path 中,这就解释了为什么 sudo 找不到它。 - robertklep
8
在Ubuntu上使用nvm - 根据@robertklep的评论,我尝试了sudo/home/${user}/.nvm/version/node/${version}/bin/npm install,并且它起作用了。 - Stav Geffen
2
通过从以下软件包重新安装解决了我的问题。https://nodejs.org/en/download/ - Marghoob Suleman
显示剩余10条评论
31个回答

8

对于CentOS用户,以下方法适用:

sudo yum install npm

1
这种方法的问题在于,在 Fedora 中,例如,在 dnf / yum 存储库中非常过时。 - ACV
3
在CentOS 7上,您需要先启用EPEL软件仓库:yum install epel-release; yum install npm - hudolejev

7
对于已安装 Node 的 Debian 系统,请输入以下命令:
curl -k -O -L https://npmjs.org/install.sh    
ln -s /usr/bin/nodejs /usr/bin/node  
sh install.sh

也在Ubuntu 20.4上解决了。 - Tropin Alexey

7
您可以通过创建 npm 和 node 的符号链接来解决此问题,这很快且简单:
sudo ln -s $(which npm) /usr/local/bin/npm
sudo ln -s $(which node) /usr/local/bin/node

我正在使用which命令获取位置,并将其作为变量传递给命令,例如:$(which npm)

我还将创建一个node的符号链接,因为npm依赖于node。

然后你就可以:

sudo npm -v

无需重新安装节点或在整个目录上进行高级文件权限调整。

这表示:ln: /usr/local/bin/node: 文件已存在 - Marcie
这意味着:ln: /usr/local/bin/node: 文件已存在 - undefined

5

只需重新安装即可。

对于RHEL,CentOS和Fedora

sudo yum remove nodejs npm
sudo dnf remove nodejs npm   [On Fedora 22+ versions]

那么

yum -y install nodejs npm
dnf -y install nodejs npm   [On Fedora 22+ versions]

太棒了!现在Node和NPM都能完美地工作了!


2

1
这是唯一一个不会引入大量其他问题的答案(比如安装两次、权限混乱等等)。 - derpedy-doo

2
我通过以下方式解决了这个问题:
apt-get install npm2deb

3
这是什么答案?npm2deb是第三方。 - Beau Bouchard
1
它并不完全可信,因为第三方可能比直接从npm源获取更容易受到攻击。我对npm2deb一无所知,但如果你想在公司使用它,请考虑他们可能会严格审计它。 - Kyle Calica-St

1

如果您已经下载了Node包并将其提取到某个地方,比如/opt,您可以在/usr/local/bin内创建符号链接。

/usr/local/bin/npm -> /opt/node-v4.6.0-linux-x64/bin/npm
/usr/local/bin/node -> /opt/node-v4.6.0-linux-x64/bin/node

1

请帮我翻译: 解决在全局安装npm包时遇到的EACCES权限错误

为了最大程度地减少权限错误的可能性,您可以配置npm使用不同的目录。在此示例中,您将创建并使用位于主目录中的隐藏目录。

备份您的计算机。 在命令行中,进入您的主目录,在其中创建一个用于全局安装的目录:

 mkdir ~/.npm-global

配置npm使用新的目录路径:

 npm config set prefix '~/.npm-global'

在您喜欢的文本编辑器中,打开或创建一个 ~/.profile 文件,并添加以下行:
 export PATH=~/.npm-global/bin:$PATH

在命令行中,更新您的系统变量:
 source ~/.profile

为了测试您的新配置,请在不使用sudo的情况下全局安装一个软件包:
 npm install -g jshint

1

对于使用以下工具的用户:

NVM 和 Homebrew

请确保设置了 Node 版本。对我而言,我的 Node 版本未被设置。因此,我使用以下命令检查我拥有哪些版本:

nvm ls

这列出了 v16.13.1,所以我将其设置为使用该版本:

nvm use 16

一旦我的 Node 版本被设置,npm 命令就能正常工作了。


0
我在Homestead也遇到了同样的问题,并尝试了多种方法。 我尝试使用以下命令:
sudo apt-get install nodejs
但是我收到了以下错误信息:
The following packages have unmet dependencies:
 npm : Depends: nodejs but it is not going to be installed
       Depends: node-abbrev (>= 1.0.4) but it is not going to be installed
       Depends: node-ansi (>= 0.3.0-2) but it is not going to be installed
       Depends: node-ansi-color-table but it is not going to be installed
       Depends: node-archy but it is not going to be installed
       Depends: node-block-stream but it is not going to be installed
       Depends: node-fstream (>= 0.1.22) but it is not going to be installed
       Depends: node-fstream-ignore but it is not going to be installed
       Depends: node-github-url-from-git but it is not going to be installed
       Depends: node-glob (>= 3.1.21) but it is not going to be installed
       Depends: node-graceful-fs (>= 2.0.0) but it is not going to be installed
       Depends: node-inherits but it is not going to be installed
       Depends: node-ini (>= 1.1.0) but it is not going to be installed
       Depends: node-lockfile but it is not going to be installed
       Depends: node-lru-cache (>= 2.3.0) but it is not going to be installed
       Depends: node-minimatch (>= 0.2.11) but it is not going to be installed
       Depends: node-mkdirp (>= 0.3.3) but it is not going to be installed
       Depends: node-gyp (>= 0.10.9) but it is not going to be installed
       Depends: node-nopt (>= 3.0.1) but it is not going to be installed
       Depends: node-npmlog but it is not going to be installed
       Depends: node-once but it is not going to be installed
       Depends: node-osenv but it is not going to be installed
       Depends: node-read but it is not going to be installed
       Depends: node-read-package-json (>= 1.1.0) but it is not going to be installed
       Depends: node-request (>= 2.25.0) but it is not going to be installed
       Depends: node-retry but it is not going to be installed
       Depends: node-rimraf (>= 2.2.2) but it is not going to be installed
       Depends: node-semver (>= 2.1.0) but it is not going to be installed
       Depends: node-sha but it is not going to be installed
       Depends: node-slide but it is not going to be installed
       Depends: node-tar (>= 0.1.18) but it is not going to be installed
       Depends: node-underscore but it is not going to be installed
       Depends: node-which but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

最后我尝试了以下命令

sudo apt-get dist-upgrade

它运行良好。

root@homestead:/usr/local/bin# npm -v
3.10.10

root@homestead:/usr/local/bin# node -v
v6.13.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接