15

I've just rebuild my Mavericks (Mac OS X Version 9.4) machine from scratch. I am the administrator and only user of this machine.

  • I installed Git via their git-2.0.1-intel-universal-snow-leopard.pkg (http://git-scm.com/downloads) in their default /usr/local/git/bin/git location.
  • I installed Node via their node-v0.10.30.pkg (http://nodejs.org/download/) in their default /usr/local/bin/node location.
  • The final step of Node's installer package included a note that they also installed NPM in their default /usr/local/bin/npm location.
  • My $PATH echoes /usr/local/git/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin (it contains /usr/local/bin).

Running npm install -g bower results in:

npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/bower'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/bower']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/bower',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/bower',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:107:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Darwin 13.3.0
npm ERR! command "node" "/usr/local/bin/npm" "install" "-g" "bower"
npm ERR! cwd /Users/Home
npm ERR! node -v v0.10.30
npm ERR! npm -v 1.4.21
npm ERR! path /usr/local/lib/node_modules/bower
npm ERR! fstream_path /usr/local/lib/node_modules/bower
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/bower'
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /Users/Home/npm-debug.log
npm ERR! not ok code 0

Running similar installations (npm install -g yo, npm install -g grunt, npm install -g nvm, npm install -g npm) results in what appears to be the same list of errors.

Several people online suggest forcing the installation via sudo, but several others warn this could cause later issues. The manuals for these packages seem not to mention requirement of sudo, and in some cases, they have advised contacting NPM when this issue arises.

Running npm cache clear does not correct the issue.

Running ls -la in the home (~) directory shows my Mac OS X username as the owner of my .npm directory: drwxr-xr-x 15 admin staff 510 Aug 11 23:53 .npm

Does anyone know what has gone wrong and how to correct this? Thank you.

James Skemp
  • 8,018
  • 9
  • 64
  • 107
2540625
  • 11,022
  • 8
  • 52
  • 58
  • This is the answer http://stackoverflow.com/questions/18212175/npm-yeoman-install-generator-angular-without-sudo/18277225#18277225 – Chaos Jan 18 '15 at 08:30

6 Answers6

16

By the way, I solved this issue by changing the owner of /usr/local to be myself, in order to allow NPM to modules in its default location without further requiring sudo for each package's installation.

sudo chown -R `whoami` /usr/local
2540625
  • 11,022
  • 8
  • 52
  • 58
  • 1
    Note that I was having a similar issue w/ an npm install failing to write to ~/.npm and it turned out that there were several modules present there owned by root from prior runs done with sudo, so I did the above to ensure my user had ownership on ~/.npm to solve. – Kyle Feb 17 '17 at 19:19
  • 8
    `chown: /usr/local: Operation not permitted` – holms Feb 11 '18 at 03:31
  • The operation should be permitted with sudo. Are you a root/admin user? Perhaps disabling SIP might help, but I don't think that should be necessary. – 2540625 Feb 12 '18 at 02:55
  • 2
    I'm also getting "Operation not permitted", even with sudo. This should-be-simple setup is a nightmare!!! – Colin Aug 28 '18 at 05:48
  • chown: /usr/local: Operation not permitted Did anyone find the solution for this ? – Shehram Tahir Oct 09 '19 at 21:36
  • Did you disable SIP? – 2540625 Oct 10 '19 at 01:02
10

EACCES is an error of not having access on doing an operation.

Taken the line: npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/bower' we can see that npm was unable to create a directory in the given location.

The reason is very likely that you are not running the command as super user. In fact, the error log is even suggesting that:

npm ERR! Please try running this command again as root/Administrator.

To run as administrator, you have to prefix those commands with sudo. That is:

sudo npm install -g bower. You will be required to type in a password for security reasons.

alandarev
  • 8,349
  • 2
  • 34
  • 43
  • 1
    Thank you for replying. I mentioned in my question that many online advise against using sudo (across the web and SO). Also many package authors suggest `sudo` shouldn't be required for global installation (e.g. [Bower's instruction](http://bower.io/#install-bower), [Yeoman's instruction](http://yeoman.io/)). Would you have any response to those? – 2540625 Aug 12 '14 at 17:31
  • You certainly do not want to allow unauthorised user write access to `/usr/local/lib` folder. If you wish to avoid using administrative permissions without screwing up permissions on root owned infrastructure, then you need to tell NPM to install modules into location owned by your user. Configuring NPM to install modules for YOU only can be seen here: http://stackoverflow.com/questions/19352976/npm-modules-wont-install-globally-without-sudo But I would not call it a true *global* anymore. – alandarev Aug 12 '14 at 18:48
0

to start fresh remove prior node.js and npm installs as well as these :

~/.npmrc ~/.npm ~/tmp ~/.npm-init.js

to install nodejs and npm as yourself NOT root do these commands (linux) :

mkdir ${HOME}/bin

download source from : http://nodejs.org/download/

cd v0.10.30/

./configure   --prefix=${HOME}/bin/nodejs

make -j8
make install

which puts it into dir defined by above --prefix

export PATH=${HOME}/bin/nodejs/bin:$PATH

NODE_PATH so node can find dir for modules otherwise npm install xxx will put newly installed module into dir in curr dir :

export NODE_PATH=${HOME}/bin/nodejs/lib/node_modules

do above AND use syntax : npm install xxxxx -g always use the -g for global

nodejs install gives you npm as well :

ls -la ${HOME}/bin/nodejs/bin
Scott Stensland
  • 26,870
  • 12
  • 93
  • 104
0

I'd recommend that you install Node.js using a version manager such as nvm. This way, you kill two birds with one stone:

  • First, you can manage multiple versions of Node.js on the same machine (which you will want to do sooner or later).
  • Second, you get around all the access problems you experience when installing Node.js without it. At least that's my experience.

Basically it's as easy as running

$ curl https://raw.githubusercontent.com/creationix/nvm/v0.24.1/install.sh | bash

from the command line.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
0

(SOLUTION ANSWER)

Hi Guys,

I had the same problem on my MacBook Pro 2018. After trie installing any dependency globally (with "-g" or "--global") I did receive an issue "EACCES" error (it's about permissions)

You could simply add "sudo" before your commands what will force it.

For example:

sudo npm install gulp -g

or

sudo npm install browser-sync -g

Enjoy!

Adam Šipický
  • 838
  • 7
  • 6
0

It's generally not advised to run sudo unless absolutely necessary.

For issues like this, NPM can solve many issues, and allow multiple node versions to be used, and selected depending upon your use case.

I had a similar issue setting up a new machine, and installing NPM took care of the issue.

karolus
  • 912
  • 7
  • 13