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


当前回答

I had the same issue,the reason for it was npm package manager was not installed while installing node. This was caused because of the following mistake: In the installation process there is a step called "Custom Setup", here you have the option to choose one of the following: 1) Node.js runtime (This is selected by default). 2) npm package manager 3) Online documentation shortcuts. 4) Add to Path. If you proceed as it is npm package manager will not be installed and hence you will get the error.

解决方案: 当你得到这些选项时,选择npm包管理器。 这对我很管用。

其他回答

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

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

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

对于MAC用户,以下步骤对我来说是有效的。

如果你得到Error for Brew,下面是你需要在终端中首先输入的命令:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Then run following commands:
$ brew update
$ brew uninstall node
$ brew install node
$ brew postinstall 

**编辑**

警告!做chmod 777是一个相当激进的解决方案。先试试这些,一次一个,其中一个管用就停止:

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


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

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

So I

// !! 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

你可以通过为npm和node创建符号链接来解决这个问题,这是快速而简单的:

sudo ln -s $(which npm) /usr/local/bin/npm
sudo ln -s $(which node) /usr/local/bin/node

我使用which来获取位置,然后将其作为变量传递给命令,例如$(npm)。

我也为node创建了一个符号链接,因为npm依赖于 节点。

然后你就可以:

sudo npm -v

不需要重新安装节点或在整个目录上使用chmod。

对我来说,上面提到的任何使用Homebrew的方法在macOS上都不起作用。因此,我使用Homebrew卸载了node,并从https://nodejs.org/en/download/下载了node包并安装了它。这招很管用。