sudo: npm: command not found

时间:2015-07-17 09:36:45

标签: node.js unix npm

I'm trying to upgrade to the latest version of node. I'm following the instructions at 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"

这对我来说很好看。我怎样才能让NPM使用sudo命令?

28 个答案:

答案 0 :(得分:118)

我不得不做

[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12} (\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2},\d{3}) \[(.*?)\] ([^ ]*) ([^ ]*) (.*)$

对我有用。

答案 1 :(得分:87)

npm文件应该在/usr/local/bin/npm中。如果它不存在,请再次使用website上的包安装node.js.这适用于我的情况。

答案 2 :(得分:70)

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

$ brew update
$ brew uninstall node
$ brew install node
$ brew postinstall 

答案 3 :(得分:47)

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

  • 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

答案 4 :(得分:22)

警告(编辑)

执行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

<强>附加

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

  • $ 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

答案 5 :(得分:16)

我必须做以下事情:

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

答案 6 :(得分:10)

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

sudo yum install npm

答案 7 :(得分:7)

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

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

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

答案 8 :(得分:4)

安装节点后的debian 输入

curl -k -O -L https://npmjs.org/install.sh    
ln -s /usr/bin/nodejs /usr/bin/node  
sh install.sh

答案 9 :(得分:3)

只需重新安装。

在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]

容易!.. 节点和npm现在都像魅力一样!

答案 10 :(得分:2)

为我工作Resolving EACCES permissions errors when installing packages globally

要最大程度地减少权限错误的可能性,可以将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

答案 11 :(得分:2)

我通过

解决了这个问题
apt-get install npm2deb

答案 12 :(得分:2)

如果您使用nvm安装了node / npm,则必须先运行nvm环境配置文件,然后才能使用任一软件包。

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

要自动运行,请包含:

source ~/.nvm/nvm.sh

在您的用户的.bash_profile文件中

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

sudo -iu jenkins npm install grunt-cli

答案 13 :(得分:1)

我遇到了同样的问题,原因是安装节点时没有安装npm包管理器。这是由于以下错误造成的: 在安装过程中有一个名为“自定义设置”的步骤,您可以在此处选择以下选项之一: 1)Node.js运行时(默认选中)。 2)npm包管理器 3)在线文档快捷方式。 4)添加到路径。 如果你继续,因为它是npm包管理器将不会被安装,因此你将得到错误。

解决方案: 获得这些选项后,选择npm包管理器。 这对我有用。

答案 14 :(得分:1)

我的解决方法是:

get

对我来说很好。

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

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

答案 15 :(得分:1)

我在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

答案 16 :(得分:1)

完全删除节点:

  brew uninstall --force node

再次安装:

brew install node;
which node # => /usr/local/bin/node
export NODE_PATH='/usr/local/lib/node_modules'

答案 17 :(得分:1)

如果您已下载节点包并提取到/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

答案 18 :(得分:1)

我在 CentOS 上也一样。

which npm给出:

$ which npm

/usr/local/bin/npm

其中

$ sudo which npm

which: no npm in (/sbin:/bin:/usr/sbin:/usr/bin)

我对Node也是如此。

所以它可能是root不检查/usr/local/bin任何二进制文件的问题。

答案 19 :(得分:0)

使用visudo和编辑&#34; secure_path&#34;

附加到sudo路径的npm二进制路径

现在&#34; sudo npm&#34;作品

答案 20 :(得分:0)

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

sudo ln -s /opt/rh/rh-nodejs8/root/usr/bin/npm /usr/local/bin/npm

答案 21 :(得分:0)

在macOS上,这对我有用:

net.train()

答案 22 :(得分:0)

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

  

〜/ .bashrc

通过使用以下命令从普通用户更改为root用户,从而成为

root用户:

  

sudo -i

然后,我必须将路径添加到将节点提取到.bashrc文件中的节点,如下所示: enter image description here

然后使用

刷新.bashrc
  

。 〜/ .bashrc

之后

npm: command not found

走了

答案 23 :(得分:0)

对我来说,上面提到的使用Homebrew的任何方法都不适用于macOS。因此,我使用Homebrew卸载了节点并从https://nodejs.org/en/download/下载了节点软件包并安装了它。它像一种魅力。

答案 24 :(得分:0)

您可以为我制作符号链接及其作品。

  1. 找到当前npm的路径

which npm

  1. 通过以下命令进行符号链接

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

  1. 测试并验证。

sudo npm -v

答案 25 :(得分:0)

不是再次安装 node,这似乎是公认的解决方案,问题是 nodejs 文件夹/usr/local 没有权限。
输入以下命令sudo chmod -R 777 /usr/local/nodejs/

答案 26 :(得分:-1)

我在 geeks for geeks 网站上找到了解决方案。 Click Here

答案 27 :(得分:-7)

安装node.js&amp;只需运行

npm install -g bower 

来自您的项目目录