23

Some of my node modules get installed but there are always these sort of issues on this particular linux mint machine

npm install 

npm ERR! Error: EACCES, open '/home/me/.npm/semver/3.0.1/package/package.json'
npm ERR!  { [Error: EACCES, open '/home/me/.npm/semver/3.0.1/package/package.json']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/home/me/.npm/semver/3.0.1/package/package.json',
npm ERR!   parent: 'gulp' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.
FutuToad
  • 2,750
  • 5
  • 36
  • 63

14 Answers14

67

This code fix it for me.

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
krozero
  • 5,929
  • 3
  • 21
  • 33
  • 2
    I've looked at multiple answers for this, and this is the only one that has worked for me – Kody R. Dec 20 '18 at 12:15
  • 2
    FYI, just running the first command solved the problem for me on WSL. However, I also had to follow the instructions here subsequently: https://stackoverflow.com/questions/29468404/gyp-warn-eacces-user-root-does-not-have-permission-to-access-the-dev-dir?answertab=active#tab-top – James Shapiro Nov 16 '20 at 09:14
  • 1
    I thought about doing this but didn't want to give ownership of those folders to any user who happened to run this script. – Jonathan Rys May 07 '21 at 01:22
  • 2
    Likely also needs `sudo chown -R \`whoami\` /usr/local/bin` for global modules with binaries. – Jesse May 09 '21 at 20:18
13

UPDATE. See this answer for a better way.


You have to set correct permissions (ownership) so npm can access your (sub)directories with your normal user permissions:

sudo chown -R $USER <directory>

where in your case <directory> is /home/me and -R is for recursive to also change ownership of all your subdirectories, which is exactly what you want. That should fix the EACCESS issue.

Sadly the advise to run the command as root/Administrator is wrong here.

You want to avoid running npm with sudo ever, as recommended by the npm creator Isaac Schlueter:

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it’s fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

See here for more details.

Community
  • 1
  • 1
Dmitri Zaitsev
  • 13,548
  • 11
  • 76
  • 110
  • 1
    Great advice. I might just add - probably not a good idea to run this on your whole `/usr` directory, at least not on MacOSX, as this can then cause further issues. (See: http://stackoverflow.com/questions/22329005). I would recommend running it on the lowest level possible, for your `npm install` command to work. Personally I ran it on `/usr/local/lib/node_modues` and it worked fine after that. – Jonathan Aug 19 '15 at 03:36
3

Add sudo before npm install . It will permit access to write .

sudo npm install [name of package].

2

in windows run cmd as administrator and then try:

npm install -g <package-name>

in mac os or linux try:

sudo npm install -g <package-name>
1

Try to use "sudo npx create-react-app app-name" it may still show error because some dependencies may be mising but a directory and the necessary files maybe created.

Abdodt
  • 138
  • 7
1

Set the correct permission to access the necessary directories.

In my case (Node & NPM Installation via Brew) on BigSur:

sudo chown -R $USER /Users/YOUR_USERNAME/node_modules
MoD
  • 564
  • 4
  • 14
  • I was able to solve my issue using this code. Is this method considered safe, or does it have the same concerns as using `sudo`? – devaent May 14 '21 at 12:57
1

If you are still facing problem after running :

sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules

Run the npm install command with :

--unsafe-perm 

which will help you in installing the package with out any problem.

Laxmikanta Nayak
  • 375
  • 2
  • 10
1

To minimize the chance of permissions errors, you can configure npm to use a different directory. In this example, you will create and use 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 a ~/.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:

npm install -g jshint

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

Sreyorn Len
  • 123
  • 2
  • 6
1

Running the code in sudo mode have fixed the problem for me

$ sudo npm install -g nativefier

And if you have related issues like package unmet dependencies check out this link

0

I understand one might be suspicious about changing the ownership of folders situated in the system files. But it's completely safe, they are meant to host processes that you can use without sudo. So I prefer this solution because it's good and will guarantee compatibility after.

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

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

If you installed a new version of node.js, you could still get some error. Try deleting the npm cache:

sudo npm cache clear
Francesco Frapporti
  • 5,195
  • 4
  • 32
  • 39
0

To permanently fix this problem, please run:

sudo chown -R 1000:1000 "/home/$USER/.npm"
Mike
  • 14,010
  • 29
  • 101
  • 161
0

After I run the following code:

sudo npm config set global = false

I came across the problem:

$ npm install -S cors               
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules/cors
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/cors'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/cors'] 
...

And I set it back, by using this command:

sudo npm config set global = true

And then, I can install packages successfully with npm. Hope it will help.

Shaowen Zhu
  • 51
  • 1
  • 6
-1
sudo npm install -g create-react-app

just adding sudo before nmp install will give superuser privilages to run npm and that would not cause any erros while it want to alter a file or access it. well, i hope this might help you!

  • 1
    Please [edit] your answer to include an explanation of how this works and why it is the solution to the problem described in the question. See [answer]. Especially as just adding `sudo` before what is shown in the question does not result in the code you show here. Please also consider to explain the difference to other existing older and similar answers. – Yunnosch Dec 10 '20 at 16:45
  • I didn't dare to try `sudo` myself, although it seems like it should work for most people in most cases. Simply adding `sudo` to force run commands that previously failed due to lack of permission is very risky, security-wise, because if a malicious software manage to sneak itself to run along with the command somehow, it would have permission to access our system's files. I have managed run `npm install -g ...` without needing `sudo` while setting up a new computer not long ago, so unless someone has a tight deadline to deliver a project, I advise *against* trying this at all. – GlyphCat Dec 03 '21 at 05:54
-1

I had the Similar Problem when I typed:

npm install -g create-react-app

The Terminal replied:

npm ERR! code EACCES
....

So I add "sudo" like This:

sudo npm install -g create-react-app

And problem Solved!! :-) (So I am agree with "chethan chandan" about using "sudo")

Ehsan Paknejad
  • 154
  • 1
  • 7