npm安装不起作用,抛出错误。

6

我正在尝试在nodejs中安装yo和其他一些软件包,但是我一直在遇到错误。由于我是node的新手,所以有些迷茫。 我使用的操作系统是Mac OS X 10.10.3,我使用的命令是

sudo npm install --global yo

这样做会产生以下错误。
> spawn-sync@1.0.13 postinstall /usr/local/lib/node_modules/yo/node_modules/cross-spawn/node_modules/spawn-sync
> node postinstall

shell-init: error retrieving current directory: getcwd: cannot access parent directories: Permission denied
node.js:720
    var cwd = process.cwd();
                      ^
Error: EACCES, permission denied
    at Error (native)
    at Function.startup.resolveArgv0 (node.js:720:23)
    at startup (node.js:63:13)
    at node.js:814:3
npm ERR! Darwin 14.3.0
npm ERR! argv "node" "/usr/local/bin/npm" "install" "--global" "yo"
npm ERR! node v0.12.7
npm ERR! npm  v2.11.3
npm ERR! code ELIFECYCLE

npm ERR! spawn-sync@1.0.13 postinstall: `node postinstall`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the spawn-sync@1.0.13 postinstall script 'node postinstall'.
npm ERR! This is most likely a problem with the spawn-sync package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node postinstall
npm ERR! You can get their info via:
npm ERR!     npm owner ls spawn-sync
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/XXXXXXX/npm-debug.log

你是如何安装Node的? - a0viedo
3个回答

16
一般情况下,您不应该使用sudo来安装node模块。相反,使用sudo修复您的文件夹权限以允许您进行全局npm安装。
使用以下命令获取.npm目录的所有权:
sudo chown -R $(whoami) ~/.npm

同时给予 node_modules 目录写入权限

sudo chown -R $USER /usr/local/lib/node_modules

然后再尝试运行npm install命令,不要使用sudo。


我仍然遇到相同的错误。有什么建议吗?我偶然发现了这篇文章https://dev59.com/rmQo5IYBdhLWcg3wUd-L。我也重新安装了node...但没有运气。 - ryndshn
你最初使用sudo安装了任何软件吗? - snozza
1
尝试运行npm install --global --unsafe-perm yo进行安装。 - snozza
不用担心,很可能是权限问题。作为最后的努力,请尝试按照以下步骤操作:https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md 这些人曾经遇到过类似的问题,通过上述指南解决了:https://github.com/yeoman/yo/issues/253 希望这能解决问题。请告诉我们结果如何。 - snozza
顺便提一下,我刚刚在我的用户目录中检查了node_modules文件夹,其中包含yo的文件夹和其他安装时出错的软件包。这是否意味着它们已经正确安装了? - ryndshn
显示剩余2条评论

5

根据您在Reddit上的帖子,我知道sudo npm cache clean解决了您的问题。有人应该在这里回答,以便其他寻求解决方案的人可以找到它。


1
运行以下命令应该可以解决问题。
sudo npm cache clean
sudo npm install -g yo 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接