NPM won't install any package on Mac. New, cle

2020-06-01 11:41发布

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.

6条回答
Lonely孤独者°
2楼-- · 2020-06-01 12:18

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.

查看更多
一夜七次
3楼-- · 2020-06-01 12:32

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
查看更多
叼着烟拽天下
4楼-- · 2020-06-01 12:35

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.

查看更多
别忘想泡老子
5楼-- · 2020-06-01 12:39

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
查看更多
Melony?
6楼-- · 2020-06-01 12:42

(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!

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-06-01 12:43

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.

查看更多
登录 后发表回答