708

What might be causing the error Error: EACCES: permission denied, access '/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!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules' }
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/macbookmd101/.npm/_logs/2018-02-21T16_26_08_421Z-debug.log
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
hendra dedi
  • 7,124
  • 3
  • 9
  • 10
  • 27
    See the official guide by NPM on how to resolve this: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally – Dzhuneyt Apr 10 '19 at 18:10
  • Could you change the selected right response? It is completely wrong (even with nearly 1K votes) and dangerous as well. People copy&paste without knowing what they are really doing so don't lead them to mess up the system. – angelcervera Sep 20 '22 at 06:49

42 Answers42

1091

Change your file permissions... Like this

First check who owns the directory

ls -la /usr/local/lib/node_modules

it is denying access because the node_module folder is owned by root

drwxr-xr-x   3 root    wheel  102 Jun 24 23:24 node_modules

so this needs to be changed by changing root to your user but first run command below to check your current user How do I get the name of the active user via the command line in OS X?

id -un OR whoami

Then change owner

sudo chown -R [owner]:[owner] /usr/local/lib/node_modules

OR

sudo chown -R ownerName: /usr/local/lib/node_modules

OR

sudo chown -R $USER /usr/local/lib/node_modules
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
okandas
  • 11,570
  • 2
  • 15
  • 17
  • 9
    There were cases when adding sudo in command also did not worked. This worked for me and should be accepted answer. – Niroj Shr Dec 05 '18 at 05:01
  • This should be the accepted answer due to permissions of other files that you own accessing the globally installed npm modules. – metal_jacke1 Dec 23 '18 at 05:37
  • Great answer. I had forgotten this! Adding to the calls for this to be accepted answer. Using sudo should be discouraged. For Linux and MacOS this is the best approach IMO assuming one primary user of node.js on that machine. Might want to show novices how to change all child subdirectories as well since they may get cascading permission problems. – Rich Sadowsky Jan 16 '19 at 20:32
  • 1
    My company uses Microsoft Active Directory Single Sign On. So the group name has slashes in it and a space in it. Which makes this a little harder to do. The command I used was `sudo chown -R myuser:Company\\Domain\ Users /usr/local/lib/node_modules`. To properly list the group name I did `id -gn myuser`. – Patrick Mar 14 '19 at 16:13
  • 72
    This was helpful but when I run the command to change the owner I got `illegal group name`. So I changed the command to `sudo chown -R ownerName: /usr/local/lib/node_modules` and it worked. – Midori Mar 18 '19 at 07:51
  • 9
    Although thats the right answer, I have encountered this issue multiple times and the better solution is to use nvm. this way you won't need to change files owner using `chown`. https://github.com/nvm-sh/nvm – Roee Jul 23 '19 at 09:01
  • still getting the same issue, does it need system restart ? – RollerCosta Dec 03 '19 at 08:34
  • 7
    Instead of chaning the permission, its recommended in the docs to install npm with nvm. See https://stackoverflow.com/a/59575266/2311074 – Adam Jan 03 '20 at 08:31
  • command : sudo chown -R $USER /usr/local/lib/node_modules Works for me fully. – Priti Jan 26 '20 at 19:53
  • For me the 2nd command `sudo chown -R ownerName: /usr/local/lib/node_modules ` works – Arefe Mar 03 '20 at 17:30
  • I believe the first command should be changed to `ls -la /usr/local/lib/ | grep node_modules` for clarity. I ran the original command, `ls -la /usr/local/lib/node_modules`, and encountered a list of files and folders with permission and access information for different user groups, but the onscreen output were for the contents of the node_modules folder itself. The `drwxr-xr-x 3 root wheel 102 Jun 24 23:24 node_modules` output is not returned from the current command. Would you kindly update it for future readers? – Shawesome Jul 07 '20 at 23:23
  • 1
    This does not solve the problems. You should really follow the official hints to solve it on https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally#manually-change-npms-default-directory – Juergen Schulze Feb 21 '21 at 10:04
  • The best way to solve this issue (as the solution may vary from one device to another) is to read carefully the error in order to figure out which folder needs write access. In my case, I had to give /usr/local/lib and /usr/local/bin permissions. – Ahmed El-Atab Apr 14 '21 at 20:19
  • 35
    **STOP** Do https://stackoverflow.com/a/55274930/234110 instead of messing with permissions of `/usr/local/lib/node_modules` – Anand Rockzz Jul 22 '21 at 15:33
  • On Ubuntu 20.4 I was trying npm install -g @angular/cli but sudo npm install -g @angular/cli worked for me – Syed Nasir Abbas Aug 02 '21 at 21:25
  • Also do not understand why I should give access to system wide folder if I want to install modules locally for the project! – Eugen Konkov Sep 18 '21 at 10:45
  • 1
    I used the ````id -un```` to check the name of the active user. Once I found out it's not root, I didn't proceed to change the owner. I just put sudo before I run the npm command and then enter my password. My installation went smoothly. – Aye Oct 17 '21 at 22:14
  • If we ran `sudo chown -R $USER /usr/local/lib/node_modules` how do we revert it? – Stephanieraymos Nov 02 '21 at 20:25
  • @Stephanieraymos `sudo chwon -R root :wheel /usr/local/lib/node_modules` – okandas Nov 03 '21 at 07:50
  • @okandas chown: :wheel: No such file or directory – Stephanieraymos Nov 05 '21 at 16:27
  • What if another user also needs that access? – itsazzad Nov 27 '21 at 18:09
  • @Stephanieraymos typo, `sudo chwon -R root:root /usr/local/lib/node_modules` – okandas Nov 29 '21 at 10:19
520

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use a hidden directory in your home directory.

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

mkdir ~/.npm-global

Configure npm to use the new directory path:

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

In your preferred text editor, open or create the ~/.profile file and add this line:

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

On the command line, update your system variables:

source ~/.profile

To test your new configuration, install a package globally without using sudo.


Source: the NPM website.

AsukaMinato
  • 1,017
  • 12
  • 21
Naveen Raju
  • 5,411
  • 1
  • 9
  • 9
  • 95
    This is the least destructive option in my opinion. It's also [recommended in the npm website](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally#manually-change-npms-default-directory). – 17xande Apr 28 '19 at 19:23
  • 2
    Alternatively, if you don't want to do `npm config set prefix` (especially if you're writing a non-root installer), you might want to override the `PREFIX` env var or provide the `--prefix` option, both of which equivalent to `npm config set prefix`. (not sure which overrides which) – SOFe Sep 16 '19 at 02:26
  • 5
    This worked perfectly for me. In my opinion, this answer makes the least room for destructive errors and is very simple to understand and implement. – clockelliptic Oct 04 '19 at 13:41
  • The recommended solution on the NPM website is actually "Reinstall npm with a node version manager". This is an alternative. – Chuck Le Butt Jan 14 '21 at 17:20
  • 3
    To create `.profile` type `touch .profile`. To then open it in mac type `open .profile`. And `source ~/.profile` is a command by itself. – Fotios Tsakiris Nov 08 '21 at 09:12
  • 5
    if you're using `zsh` you can use `~/.zshrc` instead of `~/.profile` – Bn.F76 Apr 18 '22 at 02:19
  • For me worked if I to added to '.bash_profile' – Alvin Konda May 25 '22 at 06:30
  • Doesn't seem to work with a `nvm` install: `Your user’s .npmrc file (${HOME}/.npmrc) has a `globalconfig` and/or a `prefix` setting, which are incompatible with nvm. Run `nvm use --delete-prefix v16.18.1 --silent` to unset it.` – Pablo Bianchi Aug 29 '23 at 13:46
123

I tried the solution of the answer given by @okanda but it didn't work for me.

However it worked perfectly when I did it for several folders like mentioned in this thread: https://github.com/angular/angular-cli/issues/9676#issuecomment-464857493

sudo chown -R $USER /usr/local/lib/node_modules/
sudo chown -R $USER /usr/local/bin/
sudo chown -R $USER /usr/local/share/
Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Quentin
  • 1,361
  • 1
  • 9
  • 8
91

All you need to do is to add USER to the owner of /local/lib

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

EDIT :

To target precisely and only the node_modules folder, try using this command before using the previous one :

sudo chown -R $USER /usr/local/lib/node_modules
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Nizar
  • 1,172
  • 7
  • 10
  • 16
    If future readers do not understand permissions, then please refer to the official documentation NPM documentation and properly fix the issue. https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally. Specifically, future readers should probably **reinstall npm with a node version manager (recommended)** – ordonezalex Jun 21 '19 at 02:17
  • 1
    This is pretty straight-forward. It also means you never need to worry about anything in lib again; you own /usr/local/lib. – Obinna Nnenanya Aug 06 '19 at 08:47
  • 13
    This is **not** a good solution at all. By running above `chown` you are at high risk of destroying the permission structure of your entire filesystem. – Lars Aug 11 '19 at 16:16
  • 7
    This command ruined all of my permissions. Need to recover again all the things. – Azam Alvi Sep 15 '19 at 15:36
  • 11
    To be precise, this is not *adding* an owner. This is *changing* **the** owner, which is far more destructive than just *adding*, and is almost as destructive as `sudo chmod -R 777 /`. – SOFe Sep 16 '19 at 02:14
  • this is a horrible answer as `/usr/local/` is root for security reasons, and `/usr/local/lib/` doubly so... any hacker with access to your machine will be able to modify standard usr-installed libraries after running this command. – Tcll Aug 24 '20 at 11:18
  • DO NOT DO THIS! This is a terrible solution -- not sure why it has 55 upvotes. You essentially will break permissions on your file system and have to recover them by either reinstalling or mounting your file system on another computer and recovering. – eat-sleep-code Oct 24 '20 at 16:26
  • 1
    man this is the best for development machine on Mac I don't need to worry about security issues that's ridiculous its a developers machine – JBarros35 Nov 18 '20 at 17:09
  • 1
    it just kill my linux, i cannot do a sudo anymore!!! – Nicolas Dec 05 '20 at 21:08
  • The best way to solve this issue (as the solution may vary from one device to another) is to read carefully the error in order to figure out which folder needs write access. In my case, I had to give /usr/local/lib and /usr/local/bin permissions. – Ahmed El-Atab Apr 14 '21 at 20:19
  • If we ran `sudo chown -R $USER /usr/local/lib` how do we revert it or change it back? – Stephanieraymos Nov 02 '21 at 20:23
  • it will show `chown: cannot access '/usr/local/lib/node_modules': No such file or directory` – huykon225 Nov 03 '21 at 07:02
49

You can install npm through Node version manager or a Node installer. In the docs it states:

We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.

NPM actually recommends using a Node Version Manager to avoid these errors.

Since you have the permission error, you probably installed npm through a Node installer and now you need to reinstalled it with a nvm (node version manager).

Luckily, this is very simple. You do not even need to remove your current version of npm or Node.js.

All you need to do is

  1. Install nvm. For OSX or Linux Node use:

     curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/<VERSION>/install.sh | bash
    

where <VERSION> should be replaced with the latest version

This creates a nvm folder in your home directory.

Then

  1. Install npm and node.js through nvm. To do so, just call

     nvm install node 
    

("node" is an alias for the latest version)

Now you can install your package globally without using sudo or changing the owner of node_modules in usr folder.

Davos
  • 5,066
  • 42
  • 66
Adam
  • 25,960
  • 22
  • 158
  • 247
  • thanks for the answer Adam! just wondering what might be some possible complications later on in the build/environment set-up process? – Archy Will He 何魏奇 Jan 04 '20 at 08:26
  • also to elaborate, for people on `fish`, in order to work properly nvm needs a fish-wrapper https://github.com/FabioAntunes/fish-nvm – Archy Will He 何魏奇 Jan 04 '20 at 08:30
  • @吖奇说ARCHYSHUō I never heard of any complications when installing node with nvm. And since it is recommended from the official docs, it looks to me like the best way to go :) – Adam Jan 04 '20 at 09:33
  • 1
    Thanks! I noted that there is a new minor version of NVM, so I guess the command should be `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash` now. Also, I had to close and open Terminal to be able to run `nvm install stable`. – Johanna Jul 27 '20 at 22:25
  • As a zsh user, I had to do the following. copy the output of 'cat .bashrc | grep nvm' to my .zshrc file. Then nvm was available in my zsh. – Totem Feb 04 '21 at 14:33
  • 5
    This is the simplest and cleanest answer I've seen. It avoids messing around with folder permissions or forcing it through using "sudo". I think this should be the accepted answer. Thanks! – Christian Gossain Mar 01 '21 at 05:04
  • 1
    This resolved my problem regarding `npm install -g mongodb-realm-cli` when the same error as OP occurred. – Falnésio Aug 08 '21 at 22:07
  • 1
    Awesome! This should be the accepted answer! – DarkteK Aug 28 '21 at 15:23
  • Should we also add `export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"` into bash? – alper Dec 08 '21 at 10:10
  • @alper where do you got that from? – Adam Dec 08 '21 at 16:51
  • @Adam it shows up when I do: `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash` (first step on oyour answer) – alper Dec 08 '21 at 18:16
  • Thanks this resolved my same issues. – Andrew Biddle Jan 14 '22 at 23:23
  • What about `nvm install --lts`? Should get you latest LTS release of `node` and `npm`. – s3c Mar 27 '22 at 08:59
46

try appending sudo before whatever command you are trying.

like this : sudo npm install

Using sudo with a command in Linux/UNIX generally elevates your permissions to superuser levels. In Windows, the superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.

The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users. Some processes require you to perform actions on files or folders you don't normally have permissions to access. Installing a program that everyone can access is one of these actions.

In your case, running the installation command with sudo gives you the permissions of the superuser, and allows you to modify files that your normal user doesn't have permission to modify.

  • 83
    avoid using sudo while installing npm packages. The deeper you go with such path the harder it will be to fix it afterward. Just change permissions to npm folder as pointed in another answer and live a better life. – Lukas Liesis Oct 21 '18 at 16:22
  • 2
    That may work but it will cause continuous issues over time. Avoid this if you don't want to get all kind of permission denied errors. Like denied to npm cache folder, denied to install and so on. – Lukas Liesis Feb 13 '19 at 19:29
  • 1
    This answer should be removed as using sudo before installation of packages sets a very dangerous precedent if the user doesn't understand permissions. Please refer to the first answer in which you change ownership of core node location once. – SeaWarrior404 Jun 12 '19 at 20:33
  • or at least that's the standard illusion anyways... there are too many ways to gain root access from a user level that haven't been patched after many years, so it's safe to assume any malicious user with access to your machine can easily gain root access through any poorly secured application you have installed. – Tcll Aug 24 '20 at 11:28
  • https://ionicframework.com/docs/developing/tips#resolving-permission-errors – Ahmed El-Atab Apr 14 '21 at 20:24
  • This answer is dangerous. "Just use sudo", rather than find8ing the underlying problem is a very very bad habit. Don't be teaching people this. – Wayne Walker Nov 16 '21 at 23:56
33

If you are experiencing this problem on your Mac, Take the following steps First, use the command below to determine who owns this file.

ls -la /usr/local/lib/node_modules

You will find some files below, one of which is listed below.

drwxr-xr-x   3 root    wheel  768 May 29 02:21 node_modules

Have you noticed that the above file is owned by root? To make changes inside, you must change the path's ownership.

This command can be used to determine who the current user is.

id -un (in my case user is Yamsol)

and then you can change by calling this command (just replace your user name with ownerName)

sudo chown -R ownerName: /usr/local/lib/node_modules

in my case as you know the user is "yamsol" I will call this command this way

sudo chown -R yamsol: /usr/local/lib/node_modules

that's it.

adnan javed
  • 1,338
  • 8
  • 10
28

It looks like you're running into permission issues. If you are installing npm-packages then it might possible that you are getting an EACCES error when trying to install a package globally. This means you do not have permission to write to the directories npm uses to store global packages and commands.

Try running commands: sudo chmod u+x -R 775 ~/.npm and sudo chown $USER -R ~/.npm or you can just run any npm command with sudo, that should get resolve your issue.

If you are installing an npm-package locally, then you should be in your local project directory and can try running sudo npm install <pkg-name> command to install required package. the purpose of using sudo is that it will change your owner permissions so you can make your current user authorized to run npm commands.

I'd recommend you to take a look at https://docs.npmjs.com/getting-started/fixing-npm-permissions

Suhas Gavad
  • 1,199
  • 1
  • 8
  • 12
25

While installing global packages in ubuntu, you need special write permissions as you are writing to the usr/bin folder. It is for security reasons. So, everytime you install a global package, use:

sudo npm install -g [package-name]

For your specific case, it will be:

sudo npm install -g typescript
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
cinobili19
  • 451
  • 7
  • 10
20

Be careful with all responses that change the owner of all directories under /usr/local Basically, don't mess the Linux system!!!

Using sudo for your local stuff is a really bad recommendation as well.

The original link from www.competa.com is broken, so this is the original approach from there:

npm config set prefix ~/.npm

# open your .bashrc (Linux) or .bash_profile (Mac) file for editing:
nano ~/.bashrc # for Linux
# or...
nano ~/.bash_profile # for Mac if you haven't created a .bashrc file

# add these lines:
export PATH="$PATH:$HOME/npm/bin"
export NODE_PATH="$NODE_PATH:$HOME/npm/lib/node_modules"

# save the file and then enter this command or logout and login to make the changes take effect:
. ~/.bashrc
# or...
. ~/.bash_profile

Option B: Use a version manager like NVM

angelcervera
  • 3,699
  • 1
  • 40
  • 68
14

If it is still not working after giving permissions try running these commands:

mkdir ~/.npm-global

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

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

source ~/.profile

and finally test with this command

npm install -g jshint

This does not work for Windows.

helvete
  • 2,455
  • 13
  • 33
  • 37
Tripping
  • 375
  • 5
  • 14
  • If you follow this sequence then third line is non-permanent (will not work after restart) and fourth line is completely irrelevant and do nothing. Please see answer with most score for right solution. – Miloslav Milo Janoušek Nov 27 '20 at 12:55
12

I was trying to install react expo and apart from sudo I had to add --unsafe-perm

like this. This resolves my Issue

sudo npm install -g expo-cli --unsafe-perm
Furquan
  • 1,542
  • 16
  • 20
9

Review this carefully:

https://ionicframework.com/docs/developing/tips#resolving-permission-errors

sudo chown -R $(whoami) /usr/local/{lib/node_modules,bin,share}
sudo chown -R $(whoami) ~/.npm ~/.npmrc
Ahmed El-Atab
  • 603
  • 8
  • 20
8

Encountered in CentOS 8 stream

Solution

(1/4) Creating node_modules folder

$ sudo mkdir /usr/local/lib/node_modules

(2/4) Own with Current User

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

(3/4) Own the bin folder

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

(4/4) Own the share folder

$ sudo chown -R $USER /usr/local/share/

Paul Bradbury
  • 482
  • 6
  • 8
7

For nvm users

sudo chown -R $USER /home/bereket/.nvm/versions/node/v8.9.1/lib/node_modules 

and

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

replace v8.9.1 with your node version you are using.

bereket gebredingle
  • 12,064
  • 3
  • 36
  • 47
  • This does not worked for me! Still getting the error: npm ERR! Error: EACCES: permission denied, rename /home/samuel/.nvm/versions/node/v8.10.0/lib/node_modules/.staging/npm-90fab7c7/node_modules/columnify/node_modules/wcwidth/node_modules/defaults It appears to be something with /.staging ..that folder does not exist after the attempt of installation. Can you help me with that? – Samuel Nov 27 '19 at 19:53
  • @Samuel did u run this command ? `sudo chown -R $USER home/samuel/.nvm/versions/node/v8.10.0/lib/node_modules` – bereket gebredingle Nov 28 '19 at 05:44
6

On my mac similar issue is coming while installing Ionic.

I run the command sudo chown -R $USER /usr/local/ path you can update as given in error.

Viplav Soni
  • 1,489
  • 13
  • 18
5

Seems like you tried to install a npm package globally rather than locally, as the man npm install describes:

The -g or --global argument will cause npm to install the package globally rather than locally.

Generally, when you are setting up a npm project (among many others that you could have), it's not a good idea to install packages on Node.js global modules (/usr/local/lib/node_modules), as your the debug log suggested.

Instead of using -g, use --save, which will automatically save the package as a dependency for your package.json file:

Like this:

$ npm install express-generator --save

$ cat package.json 
{
  "name": "first_app_generator",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
},
  "author": "ivanleoncz",
  "license": "MIT",
  "dependencies": {
    "express-generator": "^4.16.0"
  }
}

But as the other answers mentioned, if you're going to use -g, you have to use sudo (if your user has sudo privileges: see /etc/sudoers) when performing npm install express-generator -g, but indeed, it's not a good idea, possibly causing permission problems.

NOTICE

There are instructions for installing express-generator with -g option, in order to have the script express-cli.js available on the system path, but you can use the locally installed script as well, located at the node_modules if your npm project:

$ ./node_modules/express-generator/bin/express-cli.js --view=pug myapp

If a message like /usr/bin/env: ‘node’: No such file or directory shows up, install nodejs-legacy (Debian/Ubuntu)

IMHO, using -g (also using sudo) is like hic sunt dracones, if you are unsure of the consequences.

For further information:

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
5

I was able to fix the issue using the following in mac.

sudo npm install -g @aws-amplify/cli --unsafe-perm=true
aldo.roman.nurena
  • 1,323
  • 12
  • 26
posha
  • 881
  • 4
  • 16
  • 28
5

This occurred as a result of npm not being able to access your global node_modules directory locally, running

sudo chown -R Name: /usr/local/lib/node_modules

e.g.

sudo chown -R developerayo: /usr/local/lib/node_modules

fixes the issue, now you can run the command you ran again.

Shodipo Ayomide
  • 1,329
  • 13
  • 20
5

For those of you still unable to fix the problem after using the above mentioned solutions. Try this

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

That should do the trick, cheers!

dpacman
  • 3,683
  • 2
  • 20
  • 35
5

It is work 100%

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

5

Below command worked for me:

sudo npm install -g appium --unsafe-perm=true --allow-root
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Vasista TVN
  • 285
  • 3
  • 6
3

Use this command to install npm as the sudo user:

sudo npm install -g create-react-app 

instead of npm install -g create-react-a pp.

axiqia
  • 302
  • 4
  • 13
Nimmi Verma
  • 465
  • 6
  • 11
  • 1
    quoting @Lukas- using sudo while installing npm packages. The deeper you go with such path the harder it will be to fix it afterward. Just change permissions to npm folder as pointed in another answer and live a better life. – Krishna Mar 01 '20 at 04:48
  • 1
    Not a good idea. I would suggest against it. You open another can of worms this way. – Jay Mayu Aug 27 '20 at 07:27
3

Simply you can change the owner or just use sudo before you command like this

sudo chown -R [owner]:[owner] /usr/local/lib/node_modules (change owner)

or

sudo npm install -g json-server

that's it.

Yurii Verbytskyi
  • 1,962
  • 3
  • 19
  • 27
Shahid jafrey
  • 257
  • 1
  • 2
  • 9
3

You need the permission of superuser levels to install React. In Linux/Unix the superuser account is generally named 'root'.

To get superuser privilege just run the following command on your terminal:

sudo -i

and then simply run the command to install React:

npm install -g create-react-app

However, the reactjs team encourages us to use the following command instead of installing a global package.

npx create-react-app app_name
Fatema Tuz Zuhora
  • 3,088
  • 1
  • 21
  • 33
  • 1
    Using sudo is a risky proposition, some packages may be safe but others may set off to do something else and take advantage of the root privileges that you are granting. sudo runs the subsequent commands as superuser – rustyDev Dec 13 '19 at 18:59
  • Some packages like `create-react-app` need root privileges to be installed as global packages; otherwise, you won't able to install it. However, the reactjs team encourages us to use the following command `npx create-react-app app_name` instead of installing a global package. – Fatema Tuz Zuhora Dec 14 '19 at 03:41
3

For linux / ubuntu if the command

npm install -g <package_name>

npm WARN deprecated superagent@4.1.0: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header).  This notice will go away with v5.0.2+ once it is released.

npm ERR! path ../lib/node_modules/<package_name>/bin/..

npm ERR! code EACCES

npm ERR! errno -13

npm ERR! syscall symlink

npm ERR! Error: EACCES: permission denied, symlink '../lib/node_modules
/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!  { [Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>']

npm ERR!   cause:
npm ERR!    { Error: EACCES: permission denied, symlink '../lib/node_modules/<package_name>/bin/..' -> '/usr/local/bin/<package_name>'

npm ERR!      errno: -13,

npm ERR!      code: 'EACCES',

npm ERR!      syscall: 'symlink',

npm ERR!      path: '../lib/node_modules/<package_name>/bin/..',
npm ERR!      dest: '/usr/local/bin/ionic' },

npm ERR!   stack:
npm ERR!    'Error: EACCES: permission denied, symlink \'../lib/node_modules/ionic/bin/ionic\' -> \'/usr/local/bin/ionic\'',

npm ERR!   errno: -13,

npm ERR!   code: 'EACCES',

npm ERR!   syscall: 'symlink',

npm ERR!   path: '../lib/node_modules/<package-name>/bin/<package-name>',

npm ERR!   dest: '/usr/local/bin/<package-name>' }

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).


npm ERR! A complete log of this run can be found in:

npm ERR!     /home/User/.npm/_logs/2019-07-29T01_20_10_566Z-debug.log

Fix : Install with root permissions

sudo npm install <package_name> -g

realr
  • 3,652
  • 6
  • 23
  • 34
3

Helped only this:

sudo chown -R ownerName: /usr/local/lib/node_modules
nicolas asinovich
  • 3,201
  • 3
  • 27
  • 37
3

After long research i understood that nothing is required for mac OS to install angular cli just use sudo npm install -g @angular/cli your terminal will prompt password enter your password it will proceed to install cli. It worked for me.

Hemanth Paluri
  • 363
  • 3
  • 5
  • 12
3

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

joe cutter
  • 183
  • 2
  • 4
3

Similar to POsha's answer but this is what worked for me on ubuntu 19

sudo npm i -g ngrok --unsafe-perm=true --allow-root

From this link

https://github.com/inconshreveable/ngrok/issues/429

Jalpesh
  • 1,104
  • 1
  • 13
  • 25
3

Just add "sudo" before npm command. Thats it.

Farrukh Ahmed
  • 115
  • 1
  • 1
  • 9
    Please don't recommend this as it's an anti-pattern and can mess with future installations – Andrei Aug 05 '20 at 21:42
3

brew uninstall node

brew install node

sudo npm install -g "your_package" --unsafe-perm=true --allow-root

Hopes this helps someone

j.rmz87
  • 786
  • 1
  • 7
  • 18
2

I was able to get rid of this issue by installing nvm, then setting node to latest version.

  1. Install nvm using curl (for latest version go to nvm.sh)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
  1. List available node versions
nvm ls

v10.12.0
default -> v10.12 (-> v10.12.0)
node -> stable (-> v10.12.0) (default)
stable -> 10.12 (-> v10.12.0)

  1. Choose which version of node to use
nvm use v10.12

nvm is not compatible with the npm config "prefix" option: currently set to ""

  1. Run this to unset the option:
nvm use --delete-prefix v10.12.0

After following the commands above, you will be able to install react/angular in Ubuntu.

ordonezalex
  • 2,645
  • 1
  • 20
  • 33
VishnuB
  • 111
  • 4
2

Use 'sudo npm install xyz' it will work.

Sohan
  • 558
  • 6
  • 17
2

If you are running Linux(ie: Arch)

# npm -g install packageName

By default this command installs the package under /usr/lib/node_modules/npm and requires root privileges to do so.

To Allow user-wide installations

To allow global package installations for the current user, set the npm_config_prefix environment variable. This is used by both npm and yarn.

mkdir ~/.node_modules
cat ~/.profile
PATH="$HOME/.node_modules/bin:$PATH"
export npm_config_prefix=~/.node_modules

Re-login or source to update changes.

You can also specify the --prefix parameter for npm install. However, this is not recommended, since you'll need to add it every time you install a global package.

$ npm -g install packageName --prefix ~/.node_modules

So you dont have to chown folder permision

Hoang Do
  • 53
  • 7
1

The best way to solve this issue (as the solution may vary from one device to another) is to read carefully the error in order to figure out which folder needs write access.

In my case, I had to give /usr/local/lib and /usr/local/bin permissions.

Ahmed El-Atab
  • 603
  • 8
  • 20
0

I have used sudo, but it didnt solve the problem, I have fixed the issue by changing the node_modules folder permission,

sudo chmod -R 777 node_modules

If you want you can replace 777 with any other code if you dont set the permission for all user/group.

coder618
  • 848
  • 1
  • 9
  • 12
0

I got the same error and solve it using the command

sudo npm install -g expo-cli --unsafe-perm

Here in image after running the command

enter image description here

then run the following commandf to update npm

npm install -g npm@latest enter image description here

I easily solve the problem.

Muhammad Numan
  • 237
  • 4
  • 20
0

I was getting this error while starting the VITE frontend server. I resolved it by adding sudo (since it is some file level permission issue) sudo npm run dev. This is not the best/safest solution but the easiest.

Abd
  • 85
  • 7
-1

in case anyone want to make the user to have access like a root user check out this https://support.apple.com/en-us/HT204012

this supposed to be the best approach if the user account is the owner of the account

-2

in my case it works i.e i want npm link so when I hit sudo npm link it works

sudo npm install

because sudo gives you permissions to superuser levels. superuser account is usually called 'Administrator.' In Linux/Unix the superuser account is generally named 'root'.

The root user has permission to access, modify or delete almost any file on your computer. Normal user accounts can access, modify or delete many fewer files. The restrictions on a normal account protect your computer from unauthorized or harmful programs or users.

  • 4
    ***Never*** use `sudo` with `npm`. This can allow potentially malicious packages to take over your computer. – shreyasm-dev Nov 12 '20 at 19:25
  • @GalaxyCat105 Ty for this. Weird because I've been taught during deployment for example, to sudo literally everything on EC2 instances. I'm getting tangled up in some weird permission issues myself. I might need to terminate the instance and start fresh. Do you recommend, from a fresh Ubuntu box for example, only install node, npm, nodemon, nginx, git all as non-root user? If I use sudo, after pulling a git repo for example, and firing off `sudo npm install` I hit EACCES issues in regards to some packages (`bcrypt` for example)...need to get these concepts sorted. – twknab Nov 23 '20 at 22:59
-3

Simple solution for linux users, just add sudo in front of whatever command you're running for npm

Amit Mishra
  • 498
  • 5
  • 16