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


当前回答

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

,多达/ .bashrc

使用命令从普通用户切换到root:

sudo -我

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

然后使用。bashrc刷新

我们走吧。~ / . bashrc

有后

npm: command not found

走了

其他回答

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

,多达/ .bashrc

使用命令从普通用户切换到root:

sudo -我

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

然后使用。bashrc刷新

我们走吧。~ / . bashrc

有后

npm: command not found

走了

npm文件应该在/usr/local/bin/npm.如果没有,请在他们的网站上重新安装node.js。这对我来说很管用。

在全局安装包时解决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

我的解决方案是:

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

对我来说没问题。

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

这是因为您更改了默认的全局包目录

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包管理器。 这对我很管用。