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

我想在nodejs中安装哟和其他一些软件包,但是我不断收到错误。 我是新来的节点,所以我有点失落。 我正在运行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 

一般来说,你不应该使用sudo来安装节点模块。 相反,使用sudo来修复您的文件夹权限,以允许您执行全局npm安装。

取得.npm目录的所有权

 sudo chown -R $(whoami) ~/.npm 

并使用以下语句写入node_modules目录的权限

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

然后尝试再次运行您的npm安装命令没有 sudo。

从你的Redditpost,我知道, sudo npm cache clean解决了你的问题。 想象一个人应该在这里回答,以便其他人想要解决这个问题可以find它。

运行以下命令应该可以解决问题。

 sudo npm cache clean sudo npm install -g yo