我刚刚通过nodejs.org上的包安装了node和npm,每当我尝试使用npm搜索或安装某个东西时,它会抛出以下错误,除非我执行命令。我觉得这是权限问题?我已经是管理员了。

npm ERR! Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR!  { [Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/Users/chietala/.npm/-/all/.cache.json' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 12.2.0
npm ERR! command "node" "/usr/local/bin/npm" "search" "bower"
npm ERR! cwd /Users/chietala
npm ERR! node -v v0.10.4
npm ERR! npm -v 1.2.18
npm ERR! path /Users/chietala/.npm/-/all/.cache.json
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/chietala/npm-debug.log
npm ERR! not ok code 0

当前回答

John Papa指出了这个问题背后的历史和原因,并给出了一个可靠的解决方案:

John Papa的步骤是:

使用brew安装没有npm的节点更新.bash_profile/.bashrc,让npm和node知道安装和查找包的位置使用brew更新节点,使用npm更新自身

希望这能帮助好奇的人!

其他回答

其他答案是建议将系统目录的所有权或权限更改为特定用户。我强烈反对这样做,这可能会变得非常尴尬,可能会搞乱整个系统!

这里有一种更通用、更安全的方法,也支持多用户。

为节点用户创建新组,并将所需用户添加到此组。然后将依赖于节点的文件/目录的所有权设置为此组。

# Create new group
sudo groupadd nodegrp 

# Add user to group (logname is a variable and gets replaced by the currently logged in user)
sudo usermod -a -G nodegrp `logname`

# Instant access to group without re-login
newgrp nodegrp

# Check group - nodegrp should be listed as well now
groups

# Change group of node_modules, node, npm to new group 
sudo chgrp -R nodegrp /usr/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm

# (You may want to change a couple of more files (like grunt etc) in your /usr/bin/ directory.)

现在,您可以以用户身份轻松安装模块

npm install -g generator-angular

某些模块(咕噜、弓箭手、溜溜球等)仍需要作为根安装。这是因为它们在/user/bin/中创建符号链接。

Edit

3年后,我建议使用节点版本管理器。它为你节省了很多时间和麻烦。

没有人提到这一点,但实际上不需要弄乱权限或单独的npm安装,只需为命令指定不同的缓存文件夹即可解决问题

npm install --cache .npm
npm run build --cache .npm

这将创建一个本地.npm文件夹

在没有sudo的情况下,我在NPM模块上也遇到了类似的问题。问题是,当我安装节点时,我通过chris/lea ppa repo使用sudo进行了安装。

我的解决方案是卸载节点,然后按以下方式安装:

从nodejs.org#下载最新的稳定节点源代码,在我的例子中是node-v0.10.20.tar.gz

tar-zxf node-v0.10.20.tar.gz#解压缩源cd node-v0.10.20#输入未压缩文件夹sudo chown$USER-R/usr/local./configure--prefix=/usr/local&&make&&makeinstall

PD:如果您不想更改/usr/local文件夹的所有权,可以将其安装在您已经拥有的位置。这种方法的问题是,您必须将安装文件夹与bash命令行绑定,以便稍后使用node命令

mkdir~/opt./configure--prefix=~/opt&&make&&makeinstallecho'export PATH=~/opt/bin:${PATH}'>~/.bashrc#或~/.profile或~/.bash _profile或~/.zshenv,具体取决于当前操作系统

使用这两种方法中的任何一种,您都可以在不使用sudo的情况下执行以下操作

npm安装-g模块安装

我在安装凹槽时遇到了这个问题(https://github.com/twitter/recess)为Bootstrap 3编译CSS。

安装凹槽时:

-npm install recess -g

你需要解锁主目录中的权限,比如Noah说:sudo chown-R`whoami`~/.npm您还需要对node_modules目录的写入权限,如Xilo因此,如果它仍然不起作用,请尝试:sudo chown-R“whoami”/usr/local/lib/node_modules如果仍然看到错误,可能还需要更正/usr/local权限:sudo chown-R“whoami”/usr/local

请注意,如本文所述,如果您在Mac上,/usr/local/实际上不是一个系统目录,因此,这个答案实际上对Mac用户来说非常“安全”。然而,如果您使用的是Linux,请参阅Christopher Will下面的答案,以获得一个多用户友好、系统目录安全(但更复杂)的解决方案。

事实上,我也有同样的问题。我在运行Ubuntu。我的问题出现了,因为我丢失了Ubuntu的公钥。甚至没有更新我的系统。它给出了GPG错误。在这种情况下,您可以使用以下命令重新获取密钥:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <key in GPG error>

之后,npm工作正常!