我正在尝试升级到最新版本的节点。我在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命令?


当前回答

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

其他回答

我的解决办法是登录为根,现在我不必再使用sudo

su root

为了在sudo权限下全局安装npm包,/usr/bin/npm应该是可用的。如果npm存在于其他目录,创建一个像这样的软链接:

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

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

我用……解决了这个问题

apt-get install npm2deb

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

在全局安装包时解决EACCES权限错误

为了尽量减少权限错误的机会,你可以配置npm使用不同的目录。在本例中,您将在主目录中创建和使用隐藏目录。

备份你的电脑。 在命令行中,在你的主目录中,创建一个用于全局安装的目录:

 mkdir ~/.npm-global

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

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

在您首选的文本编辑器中,打开或创建~/。配置文件,并添加这一行:

 export PATH=~/.npm-global/bin:$PATH

在命令行,更新你的系统变量:

 source ~/.profile

要测试您的新配置,请在不使用sudo的情况下全局安装一个包:

 npm install -g jshint