6

I am trying to install yo and a few other packages in nodejs, however I keep getting errors. I am new to node so I am a little lost. I am running Mac OS X 10.10.3. The command I am using is

sudo npm install --global yo

Doing this gives me the following errors.

> 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
ryndshn
  • 692
  • 2
  • 13
  • 30

3 Answers3

16

In general, you should not be using sudo to install node modules. Instead, use sudo to fix your folder permissions to allow you to do global npm installs.

Take ownership of the .npm directory with

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

And write permission for the node_modules directory with

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

Then try to run your npm install command again without sudo.

snozza
  • 2,123
  • 14
  • 17
  • I am still having the same error. Any suggestions? I stumbled upon this post http://stackoverflow.com/questions/16151018/npm-throws-error-without-sudo. I reinstalled node also... no luck. – ryndshn Jul 21 '15 at 18:12
  • did you install anything with sudo originally? – snozza Jul 21 '15 at 18:20
  • I don't believe so. Is there anyway to completely remove node and reinstall it? – ryndshn Jul 21 '15 at 18:22
  • 1
    try `npm install --global --unsafe-perm yo` – snozza Jul 21 '15 at 18:22
  • no worries. Quite positive that it is a permissions issue. As a last ditch effort, try follow this: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md These people had a similar issue to you which was solved by the above guide: https://github.com/yeoman/yo/issues/253 Hopefully that does the trick. Let us know how it goes – snozza Jul 21 '15 at 18:37
  • Side note, I just checked node_ modules in my user directory, and it contains a folder for yo. And other packages that gave me errors upon installation. Does this mean that they have installed correctly? – ryndshn Jul 21 '15 at 18:37
  • Thank you! Related: this answer fixed my issue installing node modules on Mac OS X outlined here: http://stackoverflow.com/questions/31548980/cannot-npm-install-mean-cli-module-on-mac-os-x – Mark Barrasso Jul 22 '15 at 13:51
5

From your Reddit post, I know that sudo npm cache clean fixed the issue for you. Figured someone should answer here so anyone else looking to solve this can find it.

Aweary
  • 2,302
  • 17
  • 26
1

Running the following commands should solve the problem.

sudo npm cache clean
sudo npm install -g yo