Ostrov
Ostrov

Reputation: 129

Permission denied when installing Browsersync via NPM

I can't install Browsersync https://www.browsersync.io/#install to my Mac OS Sierra. I installed node.js and after running command

npm install -g browser-sync

I'm getting error. What I'm doing wrong?

pavels-iMac:~ Pavel$ npm install -g browser-sync

npm ERR! tar.unpack untar error /Users/Pavel/.npm/browser-sync/2.17.2/package.tgz
npm ERR! Darwin 16.0.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "browser-sync"
npm ERR! node v4.6.0
npm ERR! npm  v2.15.9
npm ERR! path /usr/local/lib/node_modules/browser-sync
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall mkdir

npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/browser-sync'
npm ERR!     at Error (native)
npm ERR!  { [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/browser-sync']
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules/browser-sync',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/browser-sync',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack: 
npm ERR!    [ '/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:35:25',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:47:53',
npm ERR!      'FSReqWrap.oncomplete (fs.js:82:15)' ] }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

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

Upvotes: 0

Views: 2623

Answers (4)

sudo chown -R $(whoami) $(brew --prefix)/*

is sufficient to run.

Upvotes: 0

Muhammad Asad
Muhammad Asad

Reputation: 3793

I was having similar issue on Mac macOS Sierra.

I suggest using safer approach which I followed and it worked i.e. Instead of CHOWN whole /usr/local folder.

If you have installed Node via Homebrew then you will have it already just fine, you just need to CHOWN the node_modules folder i.e.

sudo chown -R $(whoami) /usr/local/lib/node_modules

So we fix the permissions for the only directory we are having issue with.

Upvotes: 1

msanford
msanford

Reputation: 12237

There are two officially-suggested methods for fixing this:

  1. Direct npm to install modules globally somewhere your user owns by issuing npm config set prefix ~./some/folder and adding ~./some/folder/bin to your $PATH.

  2. Change the ownership of a subset of /usr/local to your user.

    sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

However, the first solution seems the most sound. I don't necessarily think one should be mucking around with system permissions anyway.

Further discussion on How/why does npm recommend not running as root?

Upvotes: 2

sethasaurus
sethasaurus

Reputation: 142

It's commonly recommended to give ownership of /usr/local/ to your user account. This location is used by npm global modules as well as many mac Homebrew packages.

sudo chown -R $(whoami) /usr/local

This should fix your problem and also prevent you from having to use sudo unnecessarily.

Upvotes: 1

Related Questions