Error "Your cache folder contains root-owned files, due to a bug in previous versions of npm" while "npx create-react-app example_app"
Asked Answered
E

27

55

When I was trying to create a new react app using npx create-react-app example_app, I get this error

[Your cache folder contains root-owned files, due to a bug in previous versions of npm which has since been addressed sudo chown -R 0:0 "/root/.npm"]

I even tried to re-install create-react-app again using npm i create-react-app, it is giving the same kind of error.

I assume after searching about it that it is due to some permissions issue. My current user doesn't have permission to /home/shubham/.npm where shubham is my username, but I am not so sure about this.

I have tried to solve this error using chown command as

sudo chown -R <username>:<groupname> /home/shubham/.npm" 

where <username> is Shubham and <groupame> is 1000, but it is still not working.

Request people to help me out. If you need any more information, let me know.

Eckmann answered 21/12, 2019 at 16:15 Comment(1)
Had the same thing inside Docker. Merely restarting the container fixed it.Lalla
D
98

This worked for me:

sudo npm cache clean --force 
Doorbell answered 12/2, 2020 at 16:22 Comment(2)
Thanks for commenting. I tried with the given command, still getting same errorEckmann
This worked for me! Thank you! Here is the reason for those interested. coder-coder.com/npm-clear-cache/….Fenner
H
17

In case you're facing this issue and don't have root access to fix the permissions for the current NPM cache folder, one way to go around the error is by defining a different cache dir.

First, create a new directory for the cache. Then, set the npm_config_cache environment variable:

In Linux

export npm_config_cache=/path/to/cache

In Windows

set npm_config_cache=/path/to/cache
Hussy answered 29/9, 2021 at 19:11 Comment(2)
Where is this set to by default, and why does it work?Outrider
This works for our case. In the Dockerfile, we needed to add ENV npm_config_cache /home/node/app/.npm. This appears to be accessible to write for the Docker containers.Nomarchy
T
10

This is what helped me after going through the above answers. I hope it helps someone else too. After re-reading the npm error prompt, I adjusted the provided command to include the .npm path given in the error message. Below is a copy of my original error messages.

npm ERR! code EACCES

npm ERR! syscall access

npm ERR! path /home/vagrant/.npm-global/lib/node_modules

npm ERR! errno -13

npm ERR!

npm ERR! Your cache folder contains root-owned files, due to a bug in

npm ERR! previous versions of npm which has since been addressed.

npm ERR!

npm ERR! To permanently fix this problem, please run:

npm ERR! sudo chown -R 900:900 "/home/vagrant/.npm"

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

npm ERR! /home/vagrant/.npm/_logs/2020-07-09T16_43_35_046Z-debug.log

[ExecStack] Exit code 243 Time 01:03

[error] Executing target-hook frontend-reqs failed.

[error] Command source:build:frontend-reqs exited with code 1.

Instead of using sudo chown -R 900:900 "/home/vagrant/.npm", I used sudo chown -R 900:900 "/home/vagrant/.npm-global". After that, I was able to use the previous command that caused the error with no issues.

Turmoil answered 9/7, 2020 at 17:53 Comment(0)
C
8

Try sudo chown -R 1000:1000 "/home/shubham/.npm"

Creamery answered 21/12, 2019 at 16:19 Comment(5)
Tried with this command, still getting the same error. [ npm ERR! Your cache folder contains root-owned files, due to a bug in previous versions of npm which has since been addressed. To permanently fix this problem, please run: sudo chown -R 0:0 "/home/shubham/.npm" ]Eckmann
Did you also clean npm cache?Creamery
I have cleaned npm cache using "npm cache clean --force" as "npm cache clean" was giving error "As of npm@5, the npm cache self-heals". But now when i tried to run "npx create-react-app example_app”, it gets stucked while creating the app in middle of progress bar.Eckmann
Thanks for commenting. I tried with the given command, still getting same errorEckmann
this one worked out for meGeniagenial
D
4

If you are using CI and share the CI slave with other teams, maybe not possible to sudo chown -R {group}:{user} "~/.npm" (i.e.: using Github Enterprise with Github Actions on shared runners

I add

# use local cache to work-around 'npm ERR! path ~/.npm/_locks/staging-xxx.lock' in GHA
cache=./npm-cache

into my .npmrc in the repo to solve the issue by using local npm cache dir

Dierolf answered 6/8, 2021 at 1:16 Comment(0)
F
4

I was getting this while running npx @11ty/eleventy as root:

npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 1000:1000 "/root/.npm"

No, /root/.npm is supposed to be owned by root. It's not at all due to a bug in a previous version of npm. And yes, the root user has permission to read and write that directory. So this is a bug in the current version of npm that prevents building the project as root.

Of course following the nonsensical instructions to change the root homedir's ownership does not work.

In my case, I was running it as root as part of a build script that needs root for an rsync to complete:

#!/bin/bash -eu
# mybuildscript

npx @11ty/eleventy
rsync _site/ /srv/www/ # This needs to be run as root

So my workaround was to run that single line as an unprivileged user using sudo:

#!/bin/bash -eu
# mybuildscript

sudo -u ki9 npx @11ty/eleventy
rsync _site/ /srv/www/ # This needs to be run as root

I am using npm version 7.5.2. Hopefully this will be addressed in a future version of npm.

Frascati answered 2/10, 2022 at 17:33 Comment(0)
C
3

I had the same issue and several others while trying to update my npm packages. I will be honest I do not have enough knowledge of package handlers or why this fixed my situation, but I believe my version of npm and npx were causing the issues. I had errors immediately trying to install npx, node and create-react-app with my old and out of date version of npm.

I could not run npm install npx -g without --force. Once I did that I also ran npm install npm -g --force. After this I could already tell things were different.

After npm and npx were completely overwritten, I ran npm install node -g and npm install create-react-app -g without any weird problems. I created a new react app and started it error free.

(I also ran the cache command before all of this. sudo npm cache clean --force which I am unsure if it helped).

Cratch answered 19/2, 2020 at 2:26 Comment(0)
L
3

I had to do both the root folder and the root/.npm

sudo chown -R 1001:1001 "/root"
sudo chown -R 1001:1001 "/root/.npm"
Lello answered 11/10, 2021 at 22:46 Comment(2)
This will change ownership of the root user's homedir and could result in unintended consequences.Frascati
Bro, please don't do thisElburr
Y
3

I fixed mine by running:

  sudo npm cache clean --force 

followed by removing the root owned files. first, to view the file owners and group:

cd /Users/username/
ls -la

You have to check the owner of .npm, changed to non-root user:

 sudo chown -R username:group "/Users/username/.npm"

or for global .npm-global

 sudo chown -R username:group "/Users/username/.npm-global"

replace keyword: username/group accordingly

Yance answered 17/2, 2023 at 12:52 Comment(1)
Finally fixed this bug after several days, this was the only working answer. ThanksSportive
N
1

I also tried all of the above steps on my MAC Catalina, but didn't resolve my issue. These steps resolved my problem:

npm update
npx --ignore-existing create-react-app my-app

Thanks to this comment

Novosibirsk answered 10/8, 2020 at 3:7 Comment(0)
C
1

In my case nothing worked until:

sudo chown -R $USER ~/.npm-global
Cuman answered 21/4, 2024 at 8:41 Comment(0)
W
0

This will work: sudo chown -R 1000:1000 "~/.npm"

Whitnell answered 8/9, 2020 at 6:34 Comment(0)
W
0

Neither force-cleaning the cache nor using chown worked for me, but deleting the folder did work and fixed the issue.

My error was:

npm ERR! Your cache folder contains root-owned files, due to a bug in
npm ERR! previous versions of npm which has since been addressed.
npm ERR! 
npm ERR! To permanently fix this problem, please run:
npm ERR!   sudo chown -R 501:20 "/Users/aw/.npm"

The fix was to permanently erase the .npm folder in question, which I did using rm -rf:

sudo rm -rf /Users/aw/.npm

(Mandatory warning: this is a dangerous command if you typo it.)

Western answered 7/4, 2021 at 15:4 Comment(0)
S
0

I encountered the same issue and the problem with mine was that I was running an older version of npm. After updating npm and removing the .npmrc file, it worked just fine.

npm install -g npm@latest
rm .npmrc

Be careful before deleting the .npmrc file, take a backup as it will reset your npm configuration.

Shoreline answered 22/3, 2022 at 20:13 Comment(2)
There is no reason to remove your npmrc file, what you have done here is just update the npm version, not resolve the npm cache issue.Snowonthemountain
npm install -g npm@latest ... worked for meMightily
W
0

I am working in laravel with webpack not vite. And have this problem too. Also i am working with docker and i founded solution to run watch without cache.

In bash i create an alias

alias dc-watch-no-cache="docker-compose exec node npm run --cache /var/cache/ watch"

Walkon answered 25/5, 2023 at 7:46 Comment(0)
D
0

Solution that worked for me :

sudo chown -R $USER:$(id -gn $USER) /Users/Hichem/.npm-global

npm install -g @angular/cli

Donau answered 10/6, 2023 at 21:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Maladroit
T
0

I did the simple thing by adding sudo and typing the password to the command, in you case, sudo npm i create-react-app, this solved the issue, especially with company computers

Trotskyite answered 13/3, 2024 at 12:55 Comment(0)
G
0

update your npx package to the latest version use npx update in the terminal it worked for me

Hope it helps someone

Guggle answered 20/3, 2024 at 14:6 Comment(0)
L
0

I tried all but none of them worked for me. Then I downloaded it with homebrew:

brew install aws-cdk
Linhliniment answered 23/5, 2024 at 7:4 Comment(0)
M
0

I had to do two things

  1. give writable permissions to my home .npmrc file
sudo chmod 755 ~/.npmrc
  1. give recursive ownership to my home .npm directory
sudo chown -R <user>:<group> ~/.npm

Then npm config/adduser/login finally worked

Mediocrity answered 7/6, 2024 at 23:13 Comment(0)
A
0

This is what worked for me: npm config set cache /npm-cache

This sets the cache location to npm-cache folder

Found in an answer from a Github thread https://github.com/npm/cli/issues/5114#issuecomment-1196447416

Affiliation answered 28/6, 2024 at 8:11 Comment(0)
D
-1

It work for me now!

What I did? (I'm not saying it's the best way to fix this but i'ts working for me. I'm still looking for the best explications for this issue)

  sudo rm -Rf /home/[YOUR_USER_NAME]/.npm-global/lib/node_modules/

and run your command again.

My explication is that it seems like something is corrupted in the node_modules's folder. As it's not risky to delete it I did it and it works now.

Despite answered 31/12, 2019 at 13:10 Comment(1)
Thanks for commenting. I tried with the given command, still getting same error.Eckmann
B
-1

Try sudo npx create-react-app example_app or in my case sudo npm install @vue/cli

I tried everything else here, but in the end realised that it might just be the command that I was running was not able to escalate the correct permissions.

Benzophenone answered 6/9, 2020 at 8:6 Comment(0)
A
-1

Find error in your log file:

verbose Error: EACCES: permission denied, mkdir '/Users/xxx/.tnpm_tmp/npm-52731-e91f6671'

Use that error in your condition.

sudo chown -R 502:20 "/Users/XXX/.tnpm_tmp"
Amenable answered 8/5, 2021 at 10:18 Comment(0)
G
-2

It helped me: Remove and install npm again

Grandsire answered 9/3, 2020 at 15:42 Comment(2)
this is not an answer to the question, just try commenting through it, I think he already tried thatAuvil
Right,I have already tried the same. Still not able to resolve it.Eckmann
V
-2

I solve the problem with the following commands

  1. Install dependence > npm install create-react-app
  2. run > create-react-app project_name

happy hack!!

Vintner answered 7/6, 2021 at 15:47 Comment(0)
C
-8

Try to create your react app using yarn package manager.

Chatman answered 24/6, 2022 at 17:40 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Maladroit

© 2022 - 2025 — McMap. All rights reserved.