109

I am trying to install angular cli but it's show me Missing write access to /usr/local/lib/node_modules so, how can I fix it in my mac i try it may time but, not getting exact answer

 npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  { [Error: EACCES: permission denied, access '/usr/local/lib/node_modules']
npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, access \'/usr/local/lib/node_modules\'',
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator (though this is not recommended).
Kundan Kumar Mourya
  • 1,191
  • 2
  • 8
  • 11

9 Answers9

187

Run these commands in a terminal window:

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/lib/node_modules
sudo chown -R $USER /usr/local/lib/node_modules
Greg
  • 8,175
  • 16
  • 72
  • 125
jeduard
  • 1,871
  • 1
  • 7
  • 3
  • 11
    I tried this code but when I run the second command then I am getting chown: /usr/lib/node_modules: No such file or directory – questionbank Jul 24 '19 at 08:55
  • 7
    The second command might not work but its okay. First and third one is enough. At least worked for me. – Shihab Aug 02 '19 at 12:54
  • 1
    'sudo chown -R $USER /usr/lib/node_modules' - this command dont really needed , since local modules installed into /usr/local/lib/node_modules path – Tamara Koliada Feb 23 '20 at 09:29
  • 2
    Can someone confirm my understanding please ? sudo: "do this with admin powers" // chown: "update who owns this file/directory as a user/group" // -R : "this dir and all its directories & files" // $USER : a global variable that contains the user actually logged ? // path. Thanks! –  Mar 21 '20 at 18:09
  • 2
    @Dimitri - exactly correct ($USER is an environment variable set to the logged-in user's name) – danh Jun 24 '20 at 17:19
  • This solves my first issue on fresh install of node.js in mac and also it's terminal usage in VS Code – Sojimaxi Oct 14 '20 at 14:56
  • This answer doesn't work when I run npm uninstall -g package –  May 08 '21 at 10:01
  • 1
    I had to run `sudo chown -R $USER /usr/local/bin/` on my mac M1 to make it work – Fabien Sa May 24 '22 at 13:33
66

I had this issue, I could fixed it by calling sudo on every global install. I wasn't ok with that.

I followed the n package installation advices to be able to install npm packages without sudo.

sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share

Answer of @jeduard didn't work for me.

  • Optional: on another machine I had the same issue and I forgot about this solution. So I searched and found another one from npm themselves: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally . Turns out it wasn't working so easily (needed to 'source ~/.profile' time to time). I did what I've answered here and everything works like a charm. I'm just sharing another way of handling this issue. –  May 20 '20 at 08:30
  • Answer of @jeduard didn't work for me either. I'm on Ubuntu 20 – Eric Dela Cruz Dec 01 '20 at 04:13
33

Best way to solve your npm install -g issues:

https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally

This way, you will never need to use SUDO or change permissions.

Quoting:

  1. Back up your computer.

  2. On the command line, in your home directory, create a directory for global installations:

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

    npm config set prefix '~/.npm-global'
    
  4. In your preferred text editor, open or create a ~/.profile file and add this line:

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

    source ~/.profile
    
Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Rodrigo Boratto
  • 1,053
  • 13
  • 13
  • 2
    Same this worked for me. I use Iterm and ZSH so I had to do the following as well so that it will work every time I open the terminal and don't have to run source ~/.profile each time After Creating the ~/.profile I added `source ~/.profile` to my .zshrc file – Mike Jan 12 '21 at 00:14
11

Appending sudo before the npm install saved my day:

sudo npm install -g @angular/cli

Once you provide your system password, installation should finish smoothly:

Attaching Screenshot

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
pragapraga
  • 197
  • 1
  • 3
  • 11
4

Same problem. I reinstalled the latest version of node via https://nodejs.org/en/download/ instead of using terminal. This resolved the issue.

SavPhill
  • 636
  • 6
  • 24
2

I had a similar issue when trying to install React -- issues with permissions being denied.

This is where SUDO can come in handy, it allows a permitted user to execute a command as the superuser or another user. Think of like "Run as Administrator" on Windows. If you type in the following command, it should allow you to access all of the needed folders with the proper elevated access.

sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share

This is also denoted in Dimitri's answer: https://stackoverflow.com/a/60791736/3803726

Chioke Aarhus
  • 333
  • 3
  • 10
1

For macOS try to run -

sudo chown -R $USER ~/.npm
sudo chown -R $USER /usr/local/lib/node_modules
sudo chown -R $USER /usr/local/bin/

If this still doesn't work you might need to add sudo to the npm install command.

Shaun
  • 79
  • 5
0

If you're really blocked on installing NPM modules globally (e.g. on some corporate laptop), you could just install them all in a single local folder somewhere and then add the path to node_modules/.bin/ to your PATH environment.

RCross
  • 4,919
  • 4
  • 44
  • 42
0

I had the same issue.
type this cmd in terminal

sudo nautilus

after that, it opens another file manager that has root access

enter image description here

after that type

    sudo apt install nodejs 
    sudo apt install npm 
    sudo npm install -g npm@latest
    sudo npm install -g @angular/cli
    ng --version
juliomalves
  • 42,130
  • 20
  • 150
  • 146
  • The original question seems to be mac-related, while this answer seems to refer to a Debian based Linux system. This migh be helpful for others but should be mentioned explicitly. – cknoll Feb 22 '21 at 19:28