Avoiding permission issues while installing Node packages globally

Views: 80   

In the following example i am trying to install a npm package globally and i saw the following issues.

MacBook-Pro: experimental$ npm install -g nodemon
npm ERR! path ../lib/node_modules/nodemon/bin/nodemon.js
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall symlink
npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules/nodemon/bin/nodemon.js' -> '/usr/local/bin/nodemon'
npm ERR!  { Error: EACCES: permission denied, symlink '../lib/node_modules/nodemon/bin/nodemon.js' -> '/usr/local/bin/nodemon'
npm ERR!   cause: 
npm ERR!    { Error: EACCES: permission denied, symlink '../lib/node_modules/nodemon/bin/nodemon.js' -> '/usr/local/bin/nodemon'
npm ERR!      errno: -13,
npm ERR!      code: 'EACCES',
npm ERR!      syscall: 'symlink',
npm ERR!      path: '../lib/node_modules/nodemon/bin/nodemon.js',
npm ERR!      dest: '/usr/local/bin/nodemon' },
npm ERR!   stack: 'Error: EACCES: permission denied, symlink \'../lib/node_modules/nodemon/bin/nodemon.js\' -> \'/usr/local/bin/nodemon\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'symlink',
npm ERR!   path: '../lib/node_modules/nodemon/bin/nodemon.js',
npm ERR!   dest: '/usr/local/bin/nodemon' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/experimental/.npm/_logs/2017-07-31T10_28_43_538Z-debug.log

This happens mainly because you install nodejs installer as root and trying to install the npm packages with current user. To avoid this problem execute the following command.

sudo chown -R $USER /usr/local

Note: do not replace the $USER with your username use the command as is.

Once you are done with executing the above command you can start installing any node packages globally with out using sudo.

On By