sudo:npm:找不到命令

我正在尝试升级到最新版本的节点。我正在按照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://stackoverflow.com/a/5062718/1246159尝试了解决方案

但是我仍然遇到同样的错误。我还查看了/ etc / sudoers文件,相关的行是:

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

在我看来,这很好。如何使用sudo命令使NPM正常工作?

神无乐2020/03/23 21:47:28

由于我已经使用node.js上的.tar文件安装了node.js,因此必须将节点目录的位置放在:

〜/ .bashrc

通过使用以下命令从普通用户更改为root用户来确定root用户:

须藤-i

然后必须将路径添加到将节点提取到.bashrc文件中的节点,如下所示: 在此处输入图片说明

然后使用

〜/ .bashrc

之后

npm: command not found

离开了

GO2020/03/23 21:47:28

万一对使用rh-*软件包的人有用,这对我有用

sudo ln -s /opt/rh/rh-nodejs8/root/usr/bin/npm /usr/local/bin/npm
小胖2020/03/23 21:47:28

Installl node.js并简单运行

npm install -g bower 

从您的项目目录

达蒙2020/03/23 21:47:28

使用visudo并编辑“ secure_path”将npm二进制路径附加到sudo路径

现在,“ sudo npm”有效

StafanL2020/03/23 21:47:28

如果您已经下载了节点程序包并解压缩到某个位置/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
GO凯小小2020/03/23 21:47:28

我有同样的问题,原因是安装节点时未安装npm软件包管理器。这是由于以下错误引起的:在安装过程中,有一个名为“自定义安装”的步骤,在此您可以选择以下选项之一:1)Node.js运行时(默认情况下处于选中状态)。2)npm软件包管理器3)在线文档快捷方式。4)添加到路径。如果继续进行,则不会安装npm软件包管理器,因此会出现错误。

解决方案:当您获得这些选项时,请选择npm软件包管理器。这对我有用。

番长猴子2020/03/23 21:47:28

我在Homestead也遇到过同样的问题,并尝试了许多方法。我尝试过

须藤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
猪猪2020/03/23 21:47:28

我的解决方案是:

sudo -E env "PATH=$PATH" n stable

对我来说很好。

在这里找到它:https : //stackoverflow.com/a/29400598/861615

发生这种情况是因为您更改了默认全局软件包目录

阿飞西里Gil2020/03/23 21:47:28

只需重新安装。

在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现在都像一个魅力!

乐米亚2020/03/23 21:47:28

在macOS上,这对我有用:

brew reinstall node
乐米亚2020/03/23 21:47:28

我解决了这个问题

apt-get install npm2deb
猿神奇2020/03/23 21:47:28

为了以sudo权限全局安装npm软件包,/usr/bin/npm应该可用。如果npm存在于其他目录中,则创建一个软链接,例如:

sudo ln -s /usr/local/bin/npm /usr/bin/npm

它适用于Fedora 25,node8.0.0和npm5.0.0

猴子村村2020/03/23 21:47:28

如果您将节点/ npm与nvm一起安装,则必须先运行nvm环境配置文件,然后才能使用这两个软件包。

通常可以在〜/ .nvm / nvm.sh中找到。

要自动运行,包括:

source ~/.nvm/nvm.sh

在您的用户的.bash_profile文件中

然后,如果要对该用户使用sudo,请确保包含-i参数以使sudo设置用户环境。例如

sudo -iu jenkins npm install grunt-cli
西里逆天2020/03/23 21:47:28

对于安装节点后的debian,请输入

curl -k -O -L https://npmjs.org/install.sh    
ln -s /usr/bin/nodejs /usr/bin/node  
sh install.sh
猴子村村2020/03/23 21:47:28

对于CentOS用户,这对我有用:

sudo yum install npm
Tony凯2020/03/23 21:47:27

我必须做

sudo apt-get install npm

对我有用。

十三LEY2020/03/23 21:47:27

我有同样的问题; 这是修复它的命令:

  • sudo ln -s /usr/local/bin/node /usr/bin/node
  • sudo ln -s /usr/local/lib/node /usr/lib/node
  • sudo ln -s /usr/local/bin/npm /usr/bin/npm
  • sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf
乐米亚2020/03/23 21:47:27

我必须执行以下操作:

  1. brew update
  2. brew uninstall node
  3. 访问https://nodejs.org/en/下载文件
  4. 安装下载的文件
Mandy2020/03/23 21:47:27

警告(编辑)

这样做chmod 777是一个非常彻底的解决方案。首先尝试这些,一次尝试一次,然后在尝试工作时停止:

  • $ sudo chmod -R 777 /usr/local/lib/node_modules/npm
  • $ sudo chmod -R 777 /usr/local/lib/node_modules
  • $ sudo chmod g+w /usr/local/lib
  • $ sudo chmod g+rwx /usr/local/lib

$ brew postinstall node 是我遇到问题的唯一安装部分

Permission denied - /usr/local/lib/node_modules/npm/.github

所以我

// !! READ EDIT ABOVE BEFORE RUNNING THIS CODE !!
$ sudo chmod -R 777 /usr/local/lib
$ brew postinstall node

和中提琴,npm现在已链接

$ npm -v
3.10.10

额外

如果您-R 777lib上使用我的建议是将嵌套文件和目录设置为默认设置:

  • $ find /usr/local/lib -type f -print -exec chmod 644 {} \;
  • $ find /usr/local/lib -type d -print -exec chmod 755 {} \;
  • $ chmod /usr/local/lib 755
2020/03/23 21:47:27

对于MAC用户,以下步骤对我有用。

$ brew update
$ brew uninstall node
$ brew install node
$ brew postinstall 
番长猴子2020/03/23 21:47:27

npm文件应位于中/usr/local/bin/npm如果它不存在,与包装上的node.js的重新安装网站在我看来,这是可行的。