1485

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
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Chad
  • 18,076
  • 8
  • 31
  • 41
  • 42
    Please consider [the solution](http://stackoverflow.com/a/24404451/1480391) using **NVM**: http://stackoverflow.com/a/24404451/1480391 (instead of hacking with permissions) – Yves M. Jun 30 '14 at 15:57
  • 2
    @janaspage You can not install node or NVM (Node Version Manager) via [NPM](https://www.npmjs.org/) (Node Package Manager), it's non sense. NPM comes within node (it is installed at the same time). Have a look at the Wikipedia page: http://en.wikipedia.org/wiki/Npm_(software) – Yves M. Aug 19 '14 at 12:06
  • 6
    Finally a solution better than `sudo chown`: https://github.com/sindresorhus/guides/blob/master/npm-global-without-sudo.md – Dmitri Zaitsev Jan 19 '16 at 10:06
  • Under OSX and installing node with the 0official pkg intaller this solution did not work. I used this one instead: http://stackoverflow.com/a/34968008/675565 – fmquaglia Feb 04 '16 at 13:01
  • 3
    It explains the issue and the fix: https://docs.npmjs.com/getting-started/fixing-npm-permissions – n00b Oct 18 '16 at 05:54
  • Watch out! Do not only chown or chmod the directory! See: http://stackoverflow.com/a/41395398/1256697 – suther Dec 30 '16 at 11:31
  • If the platform you are using is unix then you can use npm install --unsafe-perm. Npm doc.: https://docs.npmjs.com/misc/config#unsafe-perm – Gergő Kajtár Dec 15 '18 at 22:13
  • Chad please change answer to good solution , not messing with permission as suther gave. – Rajanboy Jun 24 '22 at 10:13
  • wow I haven't checked on this in over 10 years. Changed the accepted solution. – Chad Aug 16 '23 at 01:18

39 Answers39

2468

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

sudo chown -R $(whoami) ~/.npm
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Noah
  • 33,851
  • 5
  • 37
  • 32
  • 120
    I thought that `whoami` was a placeholder, but it works typed literally as-is, so it must be a variable I don't understand. – SimplGy May 14 '13 at 05:18
  • 143
    whoami is an actual shell command http://en.wikipedia.org/wiki/Whoami. The backticks around `whoami` ensure that it gets executed correctly and then placed into the chown command – Noah May 14 '13 at 15:21
  • This is good, but there's a more complete answer for npm newbies such as myself. Might need to create a new /npm dir & also point npm to use it. See suther's answer below. – logicOnAbstractions Jan 09 '22 at 14:42
  • Using the answer below `npm config get prefix` showed that I had to do this for `~/.nvm` as well. – Markus Mar 20 '23 at 14:01
  • Thanks! This command solved a lot of issues in Mac. – sohammondal Jun 15 '23 at 09:45
736

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.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
  • 46
    Of all the solutions posted the NVM solution here provided the best results for me. Highly recommend using NVM rather than toying with permissions. – wenincode Jul 23 '14 at 02:05
  • None of these solutions worked for me. – Mr. Panda Oct 20 '21 at 12:20
  • 2
    I installed node from the snap store but I could not run commands like npm install -g. But after trying the NVM solution everything worked smoothly. Thank you very much for your answer. – kevininhe Oct 26 '21 at 04:45
  • This article was helpful as well: https://medium.com/devops-techable/how-to-install-nvm-node-version-manager-on-macos-with-homebrew-1bc10626181 – AndrewLeonardi Aug 04 '22 at 21:22
  • **WARNING** Using only `npm install node` will install the latest (non-LTS) version! To install the LTS (safer) version, use `npm install --lts` instead. – Borjovsky Apr 14 '23 at 17:51
403

Also you will need the write permission in node_modules directory:

sudo chown -R $USER /usr/local/lib/node_modules
Yves M.
  • 29,855
  • 23
  • 108
  • 144
Xilo
  • 4,927
  • 1
  • 12
  • 2
  • 146
    I don't know why this still gets upvotes. It is a very bad practice to change the ownership of system directories to a particular user! Please see answer below for other solutions (like creating a separate group for node users). – Christopher Will May 28 '14 at 11:06
  • 37
    Whatever you do -- ABSOLUTELY do not run 'sudo chmod -R whoami' on /usr/local/lib or /usr/lib/ you will ruin your sudoers file and you will hate yourself. – qodeninja Mar 10 '15 at 01:02
  • i poked around changing permissions on node-only stuff until I got an error about not being able to install a *man page*... like holy ^%$# I can't change the permissions there as that affects a *lot* more than just node! – Michael Jul 20 '22 at 19:17
129

Changing the owner on "system-global" folders is a hack. On a fresh install, I would configure NPM to use an already writable location for "user-global" programs:

npm config set prefix "${HOME}/npm"

Then make sure you add that folder to your path:

export PATH="${PATH}:${HOME}/npm/bin"

To avoid future issues, do not use sudo npm ... at all.

See @ErikAndreas' answer to NPM modules won't install globally without sudo, and longer step-by-step guide by @sindresorhus with also sets $MANPATH.

Joel Purra
  • 24,294
  • 8
  • 60
  • 60
  • Just to note the actual path for current versions of node should be `.npm` and `.npm/bin` – Chris Rutherford May 12 '22 at 16:27
  • @ChrisRutherford: as you can see the path is configurable, so it can be any accessible path you want. Chose to _not_ use a dot-name in this answer/example, both because `~/.npm` would collide with the "broken" directory in the original question, and because using a hidden directory (at least by default on *nix systems) would be harder to debug when you "just want things to work" and get on with your day. – Joel Purra May 12 '22 at 18:08
94

Watch OUT!!! Watch OUT!!! Watch OUT!!!

chown or chmod is NOT the solution, because of security-risk.

Instead do this, do:

First check, where npm point to, if you call:

npm config get prefix

If /usr is returned, you can do the following:

mkdir ~/.npm-global
export NPM_CONFIG_PREFIX=~/.npm-global
export PATH=$PATH:~/.npm-global/bin

This create a npm-Directory in your Home-Directory and point npm to it.

To got this changes permanent, you have to add the export-command to your .bashrc:

echo -e "export NPM_CONFIG_PREFIX=~/.npm-global\nexport PATH=\$PATH:~/.npm-global/bin" >> ~/.bashrc
Yves M.
  • 29,855
  • 23
  • 108
  • 144
suther
  • 12,600
  • 4
  • 62
  • 99
  • This is the best solution IMO - avoids messing with system dir ownership. INstalling with sudo can also lead to rights issues further down the road (since stuff might end up ownded by root as well...) – logicOnAbstractions Jan 09 '22 at 14:40
  • 1
    So what to do about stuff already installed globally? Especially if some of it is old and other stuff depends on that particular version? – Michael Jul 20 '22 at 19:53
  • 1
    Whenever I get hold of a new machine (either physical or virtual), this is amongst my list to to setup. Saw this last at least 4 years back, and it is still relevant. Surprisingly, it is not the Accepted answer yet – prateek_cs_2012 Mar 20 '23 at 09:39
60

I encountered this when installing Recess (https://github.com/twitter/recess) to compile my CSS for Bootstrap 3.

When installing recess:

-npm install recess -g
  1. You need to unlock permissions in your home directory, like Noah says:

    sudo chown -R `whoami` ~/.npm
    
  2. You also need write permissions to the node_modules directory, like Xilo says, so if it still isn't working, try:

    sudo chown -R `whoami` /usr/local/lib/node_modules
    
  3. If you are still seeing errors, you may also need to correct /usr/local permissions:

    sudo chown -R `whoami` /usr/local
    

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.

Community
  • 1
  • 1
danomarr
  • 745
  • 5
  • 3
  • 41
    This is a bad idea. You probably do not want system directories to be owned by a particular user. Beside serious security concerns this is also not multiuser compatible. – Christopher Will Jan 09 '14 at 11:10
  • It terrifies me that this has so many upvotes. Changing ownership of `/usr/local` is permanent system damage by definition, and can only be undone with a full OS install. It is a MASSIVE security issue, and has the potential to make some systems unbootable. This answer is as good as being a virus. I'm sorry, but I have to flag this for deletion. – aggregate1166877 May 07 '23 at 16:24
41

Other answers are suggesting to change ownerships or permissions of system directories to a specific user. I highly disadvise from doing so, this can become very awkward and might mess up the entire system!

Here is a more generic and safer approach that supports multi-user as well.

Create a new group for node-users and add the required users to this group. Then set the ownership of node-dependant files/directories to this group.

# Create new group
sudo groupadd nodegrp 

# Add user to group (logname is a variable and gets replaced by the currently logged in user)
sudo usermod -a -G nodegrp `logname`

# Instant access to group without re-login
newgrp nodegrp

# Check group - nodegrp should be listed as well now
groups

# Change group of node_modules, node, npm to new group 
sudo chgrp -R nodegrp /usr/lib/node_modules/
sudo chgrp nodegrp /usr/bin/node
sudo chgrp nodegrp /usr/bin/npm

# (You may want to change a couple of more files (like grunt etc) in your /usr/bin/ directory.)

Now you can easily install your modules as user

npm install -g generator-angular

Some modules (grunt, bower, yo etc.) will still need to be installed as root. This is because they create symlinks in /user/bin/.

Edit

3 years later I'd recommend to use Node Version Manager. It safes you a lot of time and trouble.

Christopher Will
  • 2,991
  • 3
  • 29
  • 46
  • 2
    If node is installed by sources, although multiuser would be a problem, all modules would work perfectly without the use of sudo. This is also very important because in the case of the yeoman module, people can't update generators through sudoing the yeoman application as it doesn't allow sudo execution :( – HeberLZ May 08 '14 at 04:23
  • 1
    On Linux, I typically use the built-in `staff` group to give permissions to my dev folders. Also, it's a good idea to run `chmod g+ws node_modules` to make sure that your group has read/write permission. – jackvsworld Jul 20 '15 at 22:09
32

The official documentation on how to fix npm install permissions with an EACCES error is located at https://docs.npmjs.com/getting-started/fixing-npm-permissions.

I encountered this problem after a fresh install of node using the .pkg installer on OSX. There are some great answers here, but I didn't see a link to npmjs.com yet.

Option 1: Change the permission to npm's default directory

  1. Find the path to npm's directory:

    npm config get prefix
    

For many systems, this will be /usr/local.

WARNING: If the displayed path is just /usr, switch to Option 2.

  1. Change the owner of npm's directories to the name of the current user (your username!):

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

    This changes the permissions of the sub-folders used by npm and some other tools (lib/node_modules, bin, and share).

Option 2: Change npm's default directory to another directory

There are times when you do not want to change ownership of the default directory that npm uses (i.e. /usr) as this could cause some problems, for example if you are sharing the system with other users.

Instead, you can configure npm to use a different directory altogether. In our case, this will be a hidden directory in our home folder.

  1. Make a directory for global installations:

    mkdir ~/.npm-global
    
  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'
    
  3. Open or create a ~/.profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH
    
  4. Back on the command line, update your system variables:

    source ~/.profile
    
HoppyKamper
  • 1,044
  • 9
  • 10
  • 2
    Thanks, this one worked for me as the chown soludion did not. You saved me. – fmquaglia Feb 04 '16 at 13:00
  • 1
    I love this answer's option 2. (1) It doesn't have the security vulnerabilities of using sudo, (2) it preserves architecture for other users, (3) it references the documentation, AND (4) it has the needed command line entries included. Thank you for adding! – Tom Rose Jan 15 '19 at 14:16
  • So what happens when you install something that's symlinked into `/usr/bin/`? Now only your user can access it. You log in as someone else, still no access, since the files are actually in another user's home directory! – Frans Jan 25 '19 at 16:25
  • Hi @hoppykamper - on step 3, how do you "create a profile file"? Thank you! – econobro Jan 02 '23 at 22:33
19

I ran into this issue, and while it's true that ~/.npm should be owned by your user, npm was not installing the modules there.

What actually solved my issue is these commands:

npm config set prefix ~/.npm
export PATH="$PATH:$HOME/.npm/bin"

It will make sure that all your global installation will go under this prefix. And it's important that your user owns this directory.

dev0experiment
  • 462
  • 1
  • 7
  • 22
sandor
  • 643
  • 6
  • 16
12

As if we need more answers here, but anyway..

Sindre Sorus has a guide Install npm packages globally without sudo on OS X and Linux outlining how to cleanly install without messing with permissions:

Here is a way to install packages globally for a given user.

  1. Create a directory for your global packages

    mkdir "${HOME}/.npm-packages"
    
  2. Reference this directory for future usage in your .bashrc/.zshrc:

    NPM_PACKAGES="${HOME}/.npm-packages"
    
  3. Indicate to npm where to store your globally installed package. In your $HOME/.npmrc file add:

    prefix=${HOME}/.npm-packages
    
  4. Ensure node will find them. Add the following to your .bashrc/.zshrc:

    NODE_PATH="$NPM_PACKAGES/lib/node_modules:$NODE_PATH"
    
  5. Ensure you'll find installed binaries and man pages. Add the following to your .bashrc/.zshrc:

    PATH="$NPM_PACKAGES/bin:$PATH"
    # Unset manpath so we can inherit from /etc/manpath via the `manpath`
    # command
    unset MANPATH # delete if you already modified MANPATH elsewhere in your config
    MANPATH="$NPM_PACKAGES/share/man:$(manpath)"
    

Check out npm-g_nosudo for doing the above steps automagically

Checkout the source of this guide for the latest updates.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
ptim
  • 14,902
  • 10
  • 83
  • 103
  • 2
    tx for the edit @AndyHayden :) My preferred method is suggested in comments above: use NVM! http://stackoverflow.com/a/24404451/1480391 – ptim Feb 25 '15 at 04:41
  • The only solution that worked for me that doesn't involve mucking about with permissions. I hate NPM and its idiotic permissions stupidity. Thanks for the solution! – RyanNerd Feb 01 '18 at 18:22
10

TL;DR

always use sudo -i or sudo -H when running npm install to install global packages.


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:

  • install some local packages without issue using npm install foo
  • install global package using sudo install -g foo-cli without issue
  • attempt to install local package with npm install bar
  • get frustrated at the npm designers now that you have to go chmod a directory again

When you use the -i or -H option with sudo, your home directory will be root's home directory. Any global installs will cache packages to /root/.npm instead of root-owned files at /home/me/.npm.

Just always use sudo -i or sudo -H when running npm install to install global packages and your npm permissions problems will melt away.

For good.

-- q.v. the accepted answer for fixing an already messed up npm.

rich remer
  • 3,407
  • 2
  • 34
  • 47
7

When you run npm install -g somepackage, you may get an EACCES error asking you to run the command again as root/Administrator. It's a permissions issue.

It's easy to fix, open your terminal (Applications > Utilities > Terminal)

sudo chown -R $USER /usr/local/lib/node_modules

** I strongly recommend you to not use the package management with sudo (sudo npm -g install something), because you can get some issues later **

Reference: http://foohack.com/2010/08/intro-to-npm/

Juancarlos Rodríguez
  • 1,574
  • 2
  • 12
  • 11
  • Yay! this one did it to me! after doing the other other ones above: `sudo chown -R \`whoami\` ~/.npm`, `sudo chown -R \`whoami\` /usr/local/lib` and – Regis Zaleman Dec 06 '13 at 21:05
  • 8
    This can cause permissions problems with lots of other apps, so I'd suggest *not* doing this. Why trade one can of worms for another? – Brad Parks Jun 05 '14 at 13:34
  • 1
    Or at least refine it to /usr/local/lib/node_modules. – Ken Jun 11 '14 at 18:31
  • This, this and more this. After banging my head against a wall, this did the trick. +1 – dashard Mar 21 '15 at 05:52
  • This was my issue. On a new macbook (os x 10.10.3), permissions on /usr/local/lib/node_modules were: $ ll /usr/local/lib/node_modules total 0 drwxr-xr-x 3 24561 wheel 102B Mar 31 18:19 . drwxrwxr-x 4 24561 admin 136B Mar 31 18:19 .. drwxr-xr-x 26 65534 staff 884B Apr 13 10:53 npm – tkane2000 Apr 17 '15 at 00:53
  • Or sudo chown -R $USER ~/local/lib/node_modules depending on how node was installed. – Arunabh Das Sep 12 '15 at 03:07
  • Using `sudo` is fine, but you should use the `-H` option. See my answer. – rich remer Oct 04 '18 at 22:00
6

I had a similar problem at NPM modules won't install globally without sudo, the issue was that when i installed node i did it with sudo via chris/lea ppa repo.

My solution was to uninstall node and then install it this way:

Download latest stable node sources from nodejs.org #in my case node-v0.10.20.tar.gz

tar -zxf node-v0.10.20.tar.gz #uncompress sources

cd node-v0.10.20 #enter uncompressed folder

sudo chown $USER -R /usr/local

./configure --prefix=/usr/local && make && make install

PD: If you don't want to change ownership of the /usr/local folder, you can install it somewhere you already own. The problem of this approach is that you will have to bind the installation folder with the bash command line so that we can use the node command later on

mkdir ~/opt

./configure --prefix=~/opt && make && make install

echo 'export PATH=~/opt/bin:${PATH}' >> ~/.bashrc #or ~/.profile or ~/.bash_profile or ~/.zshenv depending on the current Operative System

With either of those approaches, you will be able to do the following without using sudo

npm install -g module_to_install

Community
  • 1
  • 1
HeberLZ
  • 12,715
  • 4
  • 22
  • 24
  • 1
    I ended up using this method. Did use `sudo chown $USER /use/local` before building. Looks good so far, time to try to build atom! Thanks! – prasanthv May 07 '14 at 03:32
6

For me, execute only

sudo chown -R $(whoami) ~/.npm

doesn't work. Then, I execute too

sudo chown -R $(whoami) /usr/lib/node_modules/
sudo chown -R $(whoami) /usr/bin/node
sudo chown -R $(whoami) /usr/bin/npm

And all works fine!

user3753202
  • 517
  • 7
  • 14
6

ISSUE: You (the user) don't have the right set of permissions for the directory.

The instant way out is to run the npm install using sudo, but this may give you the same error, or improper installation.

AND changing directory ownership is not a good option, a temporary patch.


Solution/Suggestion: Change npm's Default Directory (from official docs)

Back-up your computer before moving forward.

(optional) In case you have a erroneous installation, first uninstall it:

npm uninstall <package-name>  # use sudo if you used it while installation
npm cache verify  # or, npm cache clean for npm version below 5.x.x 
  1. Make a directory for global installations:

    mkdir ~/.npm-global

  2. Configure npm to use the new directory path:

    npm config set prefix '~/.npm-global'

  3. Open or create a ~/.profile or ~/.bash_profile file and add this line:

    export PATH=~/.npm-global/bin:$PATH

  4. Back on the command line, update your system variables, or restart the terminal:

    source ~/.profile

  5. (optional) Test: Download a package globally without using sudo.

    npm install -g jshint

Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
5

For Mac (adopted from Christoper Will's answer)

Mac OS X 10.9.4

  1. System Preference > Users & Groups > (unlock) > press + :

    New Account > "Group"
    Account Name : nodegrp

    After creating the group, tick the user to be included in this group

  2. sudo chgrp -R nodegrp /usr/local/lib/node_modules/
    sudo chgrp nodegrp /usr/bin/node
    sudo chgrp nodegrp /usr/bin/npm
    sudo chown -R $(whoami):nodegrp ~/.npm

ken
  • 13,869
  • 6
  • 42
  • 36
  • 1
    I had to change a couple of the paths to /usr/local/bin but other than that this solution worked great on my OX X 10.9 machine - thanks ! – splig Oct 27 '14 at 20:31
  • Bad practice to change ownership of system directories. – rich remer Dec 17 '18 at 20:04
  • @richremer I think it depends on what you trying to achieve. if the folder is originally only accessible by the sudoer then it can be only be changed in this way. we limit the change to the folder related to npm. Would it be more safe to keep do `sudo` or change those folder group ownership so that it can be modified by normal user? If normal user is not supposed to perform any action on this folder then of course should not change the group ownership of the folder. – ken Sep 05 '19 at 10:51
  • The appropriate thing to do is install global packages with sudo, but do it with the `-H` flag. Specifically, `sudo -H npm install -g `. This ensures proper delineation of permissions for system and user folders. What you don't want to do is use `sudo npm install -g`, because this installs root-owned files to your user directory. It would be great if npm were smart enough to do this the right way, but it's not, so you have to use the `-H` sudo flag to properly install globally. – rich remer Nov 13 '19 at 19:27
4

In my case,it's because of the permission of ~/tmp.So I do:

sudo chown -R $USER ~/tmp

And it's OK!

bnPYSse
  • 397
  • 4
  • 12
4

Problem: You do not have permission to write to the directories that npm uses to store global packages and commands.

Solution: Allow permission for npm.

Open a terminal:

command + spacebar then type 'terminal'

Enter this command:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
  • Note: this will require your password.

This solution allows permission to ONLY the directories needed, keeping the other directories nice and safe.

Ericgit
  • 6,089
  • 2
  • 42
  • 53
Matt
  • 33,328
  • 25
  • 83
  • 97
  • 2
    Downvoting because setting user ownership of system folders is an anti-pattern and this does not account for that. In the special case that `npm config get prefix` returns something in the home folder, this solution will fix the permissions, but doesn't address the underlying problem of installing global packages in a safe way for a shared environment. – rich remer Dec 17 '18 at 19:57
  • This will rewrite the permissions of a huge portion of the operating system. Running this in Ubuntu for example will result in "sudo" command itself becoming unusable. – Sahand Seifi May 18 '20 at 22:00
  • changing `sudo` permission is a big NO!! – Dipu Aug 23 '20 at 12:00
3

Best solution would be this which is provided by npm documentation.


For Ubuntu suggested solution is Option#2

Brief steps:
Make a directory for global installations:
mkdir ~/.npm-global

Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
npm config get prefix can help you to verify if prefix was updated or not. The result would be <Your Home Directory>/.npm-global

Open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH

Back on the command line, update your system variables:
source ~/.profile

Instead of steps 2-4 you can also use the corresponding ENV variable (e.g. if you don't want to modify ~/.profile):

NPM_CONFIG_PREFIX=~/.npm-global


For Mac suggested solution is Option#3

On Mac OS you can avoid this problem altogether by using the Homebrew package manager

brew install node

Himanshu
  • 362
  • 3
  • 13
  • This NPM_CONFIG_PREFIX won't work. This is an error in npm-descriptiton. See my post above, how to do it in the right way: http://stackoverflow.com/a/41395398/1256697 – suther Dec 30 '16 at 11:20
3

simply try

npm rebuild

then after completion run your usual command

Baraja Swargiary
  • 381
  • 2
  • 10
2

Another great fix here to configure NPM properly, run the following commands :

npm config set prefix '~/.npm_packages'
PATH=$PATH:$HOME/.npm_packages/bin; export PATH
glemiere
  • 4,790
  • 7
  • 36
  • 60
2

In case sudo chown -R $(whoami) ~/.npm didn't work for you, or you need a non terminal command solution.

The issue is that your user account does not have write permission to node_modules folder, so you can do the following

  1. Open finder and press cmd + shift + g this will open go to folder with url

  2. Write the following path /usr/local/lib/node_modules and press go

  3. Right click on node_modules folder and choose Get Info

  4. Scroll down to sharing & permissions section

  5. Unlock to be able to make changes.

  6. Press + and add your user account

  7. Make sure that you choose Read & Write in privilege drop down

Now you should be able to install packages without sudo and permission issues should be solved

Amr Labib
  • 3,995
  • 3
  • 19
  • 31
2

Nobody mentioned this, but there is actually no need to mess up with permissions or a separate npm installation, just specifying a different cache folder to the command will fix the issue

npm install --cache .npm
npm run build --cache .npm

This will create a local .npm folder

Tofandel
  • 3,006
  • 1
  • 29
  • 48
1

This is how I solved the issue on Windows 8.1:

  • Go to your nodejs install (usually C:\Program Files\nodejs)
  • Right click node_modules folder and go to properties
  • Click the Security tab and advanced
  • At the top you will see "Owner: SYSTEM", click change
  • Enter the user you want permissions for and click Ok
  • Check the box at the bottom of the advanced settings "Replace all child object permission entries with inheritable permission entries from this object" and click ok
  • Do whatever npm install/update you need
KCaradonna
  • 760
  • 6
  • 13
1

you could try this, works on ubuntu and mac

sudo chown -R $(whoami) /usr/local/lib/node_modules
Adeojo Emmanuel IMM
  • 2,104
  • 1
  • 19
  • 28
  • 1
    I ran this and worked 100% for me on a mac: sudo chown -R $(whoami) /usr/local/lib/node_modules – leeroya Aug 20 '18 at 19:45
1

use below command while installing packages

 sudo npm install --unsafe-perm=true --allow-root
JHM16
  • 649
  • 8
  • 12
  • If you are interested, see my answer that avoids the need to have `sudo`, for example during a container build. https://stackoverflow.com/a/73167840/520567 – akostadinov Jul 29 '22 at 14:45
1

When trying to install packages globally as root, the process will fail with EUIDLOOKUP or EACCES or npm will mess up your user global packages permissions.

To understand the problem I would suggest reading this issue. In short npm tries to use the EUID of current process to run installation scripts not as root but as the original user running npm while at the same time assuming that a user running as root would use sudo so EUID will be set.

That's why answer of @JHM16 based on using sudo should work.

But this is not the case when installing packages inside a container build. In containers sudo will be more often than not missing. And build will usually just run as root.

Of course container build can be changed to run these commands as a regular user and build everything as that user. This will not make the build more secure though and could be undesirable in some situations.

So in a container build, here's how you can make npm global install work for example with the yarn package.

# setpriv --ruid 0 --euid 0 npm --unsafe-perm install -g yarn

setpriv is a small low level utility that should be available or easily installable on any linux distro.

akostadinov
  • 17,364
  • 6
  • 77
  • 85
0

Actually, I was also having the same problem. I was running Ubuntu. Mine problem arises because I'd lost my public key of the Ubuntu. Even updating my system was not happening. It was giving GPG error. In that case, you can regain your key by using this command:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <key in GPG error>

After that npm works fine!

sam100rav
  • 3,733
  • 4
  • 27
  • 43
0

John Papa points to the history and reasoning behind this issue and gives a solid fix:

John Papa's steps are to:

  1. Use brew to install node without npm
  2. Update your .bash_profile/.bashrc to let npm and node know where to install and find packages
  3. Use brew to update node and npm to update itself

Hope this helps the curious!

Mike Cluck
  • 31,869
  • 13
  • 80
  • 91
theship
  • 75
  • 1
  • 13
0

@Yves M.'s answer was very similar to my solution. Here are the commands I used, which were slightly different from his.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | bash

Then query for the latest version:

nvm ls-remote

Then install the newest version:

nvm install YOUR_VERSION_HERE

example

nvm install v5.8.0
Ryan Loggerythm
  • 2,877
  • 3
  • 31
  • 39
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:

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

Now you can globally install any module safely. Hope this helps!

yago
  • 168
  • 1
  • 11
  • If the user happens to not have these `npm` configs properly set, this will brick their system by recursively setting permissions for the entire OS. This is a really bad answer. – aggregate1166877 May 07 '23 at 16:33
0

I Solve it by changing the owner from root to my user-name

sudo chown -R me:me /home/me/.config/configstore/

change me with your user-name and group .

Salem
  • 654
  • 7
  • 24
0

I like to use ubuntu groups to achieve this instead of changing owner. Its quite simple.

  1. First install nodejs and npm using apt-get

    sudo apt-get update && sudo apt-get install nodejs npm

  2. Figure out who is logged in i.e username, run following command to see it in terminal

    whoami

  3. You can see the list of groups you are assigned by using a very simple command, normally the first group is your username itself

    groups

  4. Run following to allow access to logged in user

    sudo chmod 777 -R /usr/local && sudo chgrp $(whoami) -R /usr/local

  5. Update npm and nodejs

    npm install -g npm

You are allset, your user can run npm commands without sudo

You can also refer to this link https://askubuntu.com/a/1115373/687804

Gitesh Dalal
  • 178
  • 1
  • 7
0

I tried everything in comments, without any succses, but following command did the magic for me:

hash -r
MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
-1

This is the solution I utilized and worked. I tried utilizing whoami never worked.

sudo chown -R $USER /usr/local/lib/node_modules

then

sudo chown -R $USER /usr/local/bin/npm

then

sudo chown -R $USER /usr/local/bin/node

Casey Robinson
  • 3,268
  • 2
  • 14
  • 21
-2

I found that if you only sudo -s "it just starts up a shell with root permissions as a one step" and it really works for me. I don't know if it's a good practice or not.

I hope it helps.

Reference: https://apple.stackexchange.com/posts/14423/revisions

Community
  • 1
  • 1
-3

I set my user account as the owner of the /usr/local directory, so that can just issue normal commands in there.

sudo chown -R $USER /usr/local

Reference: http://howtonode.org/introduction-to-npm

-4

All the above is not necessary.

The issue I was having was I was using the -g when I was running NPM. I couldn't work out how I wasn't getting a 'npm_module' folder created in my project.

The solution is to run 'NPM init' This creates a 'package.json' and the 'npm_module' folder where all subsequent modules will be loaded into. When running npm DO NOT use -g use -s to update your 'package.json' file.

Here is a good video explaining

Arnold.Krumins
  • 1,055
  • 8
  • 8
  • The video requires a paid subscription. Can you summarize it and perhaps point to a free resource? And I assume you mean "npm", not "NPM". – nealmcb May 18 '15 at 03:23
  • This answer is not related to the OP's question and contains a link to a subscription-only service. If you find a solution to a problem that you can't find a StackOverflow question for, it is a good opportunity to create your own question and answer. – Tyler Hoppe Aug 17 '15 at 21:48
-7
sudo chown -R `whoami` /usr/local/lib
Dawson Toth
  • 5,580
  • 2
  • 22
  • 37
manuchap
  • 117
  • 1
  • 5