Title: Fixing npm EACCES Error: Cache Folder Permission Issues on macOS

Joseph Samuel Daniel
3 min readJul 29, 2023

--

npm install always gives ‘Your cache folder contains root-owned files, due to a bug in npm previous versions of npm which has since been addressed.’

Introduction:
If you are a developer using npm (Node Package Manager) on macOS, you might have encountered the frustrating EACCES error, which occurs when npm fails to access its cache folder due to permission issues. This error can prevent you from installing or updating packages, causing significant roadblocks in your development workflow. In this article, we will explore the root cause of the problem and provide a step-by-step solution to fix it.

Problem: npm ERR! code EACCES
When running npm commands, you might encounter the following error:

```
npm ERR! code EACCES
npm ERR! syscall rmdir
npm ERR! path /Users/samueljoseph/.npm/_cacache/index-v5
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 501:20 “/Users/samueljoseph/.npm”
npm ERR!
```

Explanation of the Problem:
The EACCES error is triggered when npm tries to access its cache folder and encounters root-owned files within it. This issue is caused by a bug in previous versions of npm, where some files were mistakenly assigned root ownership. As a result, when npm is run with regular user permissions, it fails to modify or delete these files, leading to the error.

Solution: Fixing Cache Folder Permission Issues

Step 1: Avoid Using sudo with chown
The error message suggests using `sudo chown` to change the ownership of the cache folder. However, using `sudo` to modify user-specific files is not recommended, as it can lead to security vulnerabilities. Instead, we’ll use the correct ownership without elevated privileges.

Step 2: Change Ownership of the npm Cache Folder
To resolve the EACCES error, we need to change the ownership of the npm cache folder and its contents to the current user account and group. Follow these steps:

Open a terminal window (command + spacebar on mac, type “terminal” and enter).

Use the following command to change the ownership of the cache folder and all its contents:

chown -R $USER:$GROUP ~/.npm

Here’s what the command does:

- `chown`: The command to change ownership.
- `-R`: Recursive flag, ensures all files and subdirectories within `~/.npm` are affected.
- `$USER`: A shell variable representing your current user account.
- `$GROUP`: A shell variable representing your user’s primary group.

Step 3: Addressing “Operation not permitted” Errors
When executing the `chown` command, you might encounter “Operation not permitted” errors for certain files or directories. This occurs when the system files require elevated privileges for modification. To resolve this, run the following commands:

sudo chown -R $USER:$GROUP /Users/samueljoseph/.npm/_logs
sudo chown -R $USER:$GROUP /Users/samueljoseph/.npm/node-sass

Replace `/Users/samueljoseph/` with your actual home directory if different.

Conclusion:
Encountering the npm EACCES error due to cache folder permission issues can be frustrating for developers. However, with the correct approach, we can easily resolve the problem without compromising system security. By following the step-by-step solution presented in this article, you can successfully fix the EACCES error and continue working seamlessly with npm on macOS. Happy coding!

Check out my Flutter gallery: https://www.tiktok.com/@obong_samjoe

--

--

Joseph Samuel Daniel

Mobile Developer (flutter and React Native) | Web Developer