Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix npm throwing error without sudo

I just installed node and npm through the package on nodejs.org, and whenever I try to search or install something with npm, it throws the following error unless I sudo the command. I have a feeling this is a permissions issue? I am already the admin.

npm ERR! Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json' npm ERR!  { [Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json'] npm ERR!   errno: 3, npm ERR!   code: 'EACCES', npm ERR!   path: '/Users/chietala/.npm/-/all/.cache.json' } npm ERR!  npm ERR! Please try running this command again as root/Administrator.  npm ERR! System Darwin 12.2.0 npm ERR! command "node" "/usr/local/bin/npm" "search" "bower" npm ERR! cwd /Users/chietala npm ERR! node -v v0.10.4 npm ERR! npm -v 1.2.18 npm ERR! path /Users/chietala/.npm/-/all/.cache.json npm ERR! code EACCES npm ERR! errno 3 npm ERR! stack Error: EACCES, open '/Users/chietala/.npm/-/all/.cache.json' npm ERR!  npm ERR! Additional logging details can be found in: npm ERR!     /Users/chietala/npm-debug.log npm ERR! not ok code 0 
like image 772
Chad Avatar asked Apr 22 '13 15:04

Chad


People also ask

Does npm require sudo?

In summary, setting prefix in . npmrc or simply using the --prefix will allow you to use npm install -g without sudo . Note that the setting of NODE_PATH will cause node to check this path for libraries.

Why npm install throws error?

The error in NPM, 'error package install failed, see above', can occur when the user creates a new project in Angular using Node. js using VS code. This means that NPM is corrupted in your system, and must reinstall NPM.


2 Answers

This looks like a permissions issue in your home directory. To reclaim ownership of the .npm directory execute:

sudo chown -R $(whoami) ~/.npm 
like image 172
Noah Avatar answered Sep 23 '22 16:09

Noah


Permissions you used when installing Node will be required when doing things like writing in your npm directory (npm link, npm install -g, etc.).

You probably ran Node.js installation with root permissions, that's why the global package installation is asking you to be root.


Solution 1: NVM

Don't hack with permissions, install Node.js the right way.

On a development machine, you should not install and run Node.js with root permissions, otherwise things like npm link, npm install -g will need the same permissions.

NVM (Node Version Manager) allows you to install Node.js without root permissions and also allows you to install many versions of Node to play easily with them.. Perfect for development.

  1. Uninstall Node (root permission will probably be required). This might help you.
  2. Then install NVM following instructions on this page.
  3. Install Node via NVM: nvm install node

Now npm link, npm install -g will no longer require you to be root.

Edit: See also https://docs.npmjs.com/getting-started/fixing-npm-permissions


Solution 2: Install packages globally for a given user

Don't hack with permissions, install npm packages globally the right way.

If you are on OSX or Linux, you can create a user dedicated directory for your global package and setup npm and node to know how to find globally installed packages.

Check out this great article for step by step instructions on installing npm modules globally without sudo.

See also: npm's documentation on Fixing npm permissions.

like image 45
Yves M. Avatar answered Sep 20 '22 16:09

Yves M.