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
What to me seems like the best option is the one suggested in the npm documentation, which is to first check where global node_modules are installed by default by running
npm config get prefix
. If you get, like I do on Trusty,/usr
, you might want to change it to a folder that you can safely own without messing things up the way I did.To do that, choose or create a new folder in your system. You may want to have it in your home directory or, like me, under
/usr/local
for consistency because I'm also a Mac user (I prefer not to need to look into different places depending on the machine I happen to be in front of). Another good reason to do that is the fact that the/usr/local
folder is probably already in your PATH (unless you like to mess around with your PATH) but chances are your newly-created folder isn't and you'd need to add it to the PATH yourself on your .bash-profile or .bashrc file.Long story short, I changed the default location of the global modules with
npm config set prefix '/usr/local'
, created the folder/usr/local/lib/node_modules
(it will be used by npm) and changed permissions for the folders used by npm with the command:Now you can globally install any module safely. Hope this helps!
you could try this
On Mac OS X, when installing with Homebrew's
brew install npm
, the installation path is/usr/local/share/npm/
with bothbin/
andlib/node_modules/
subfolders.Running this command to change to owner to your currently logged in user should fix it all up, and allow you to install global NPM packages without
sudo
.osx homebrew
I encountered this when installing Recess (https://github.com/twitter/recess) to compile my CSS for Bootstrap 3.
When installing recess:
You need to unlock permissions in your
home
directory, like Noah says:You also need write permissions to the
node_modules
directory, like Xilo says, so if it still isn't working, try:If you are still seeing errors, you may also need to correct
/usr/local
permissions:Please note that as indicated in this post
/usr/local/
isn't actually a system dir if you are on a Mac, so, this answer is actually perfectly "safe" for Mac users. However, if you are on Linux, see Christopher Will's answer below for a multi-user friendly, system dir safe (but more complex) solution.TL;DR
--
When you use
npm
it downloads packages to your user home directory. When you run as sudo,npm
installs files to the same directory, but now they are owned by root.So this is what happens to absolutely every single person who has ever used
npm
:npm install foo
sudo install -g foo-cli
without issuenpm install bar
npm
designers now that you have to gochmod
a directory againWhen you use the
-i
or-H
option with sudo, your home directory will beroot
's home directory. Any global installs will cache packages to/root/.npm
instead ofroot
-owned files at/home/me/.npm
.Just always use
sudo -i
orsudo -H
when runningnpm install
to install global packages and yournpm
permissions problems will melt away.For good.
http://hood.ie/blog/why-you-shouldnt-use-sudo-with-npm.html
-- q.v. the accepted answer for fixing an already fucked
npm
.This is how I solved the issue on Windows 8.1: