Fixing Node.js EACCES: Permission Denied Error

intermediate๐Ÿ’š Node.js2026-03-16| Node.js, npm, Linux (Ubuntu, Debian, Fedora), macOS (Ventura, Sonoma, etc.)

Error Message

Error: EACCES: permission denied

Error: EACCES: permission denied

Encountering an EACCES: permission denied error can be a major headache for Node.js developers, especially when using npm (Node Package Manager). This error means that Node.js or npm attempted to access a file or directory but didn't have the necessary permissions. You'll typically see it when installing global packages, running scripts, or working with project-specific node_modules folders.

Explanation and Root Causes

At its core, EACCES is an operating system error, specifically related to file system access. When you see "permission denied," it means your current user account lacks the necessary read, write, or execute privileges for a particular operation. For Node.js and npm users, here are the most common scenarios:

  • Global npm package installation: By default, npm attempts to install global packages into system-wide directories. For example, this is often /usr/local on macOS, or /usr/bin and /usr/local/bin on Linux. These locations typically demand root (administrator) privileges to make changes. If you try installing a global package without sudo, you'll quickly run into a permission issue.
  • Incorrect permissions from previous sudo usage: Have you ever run npm install -g or even a local npm install with sudo? If so, some files and directories might have been created or altered with root ownership. Later, when you try to manage these files as a regular user, you'll inevitably hit EACCES errors.
  • Corrupted npm cache: Occasionally, your npm cache can become corrupted. It might contain files with incorrect permissions, which then cause problems during subsequent package installations.
  • Project-specific node_modules or build directories: While less frequent, you might face this error within a specific project. If that project's node_modules directory or its build output folders are owned by a different user, or have overly restrictive permissions, you'll likely encounter problems when running local scripts or building the application.

Step-by-Step Fixes

Option 1: Configure npm's Default Directory (Recommended for Global Packages)

This approach is the safest and most highly recommended way to handle global npm packages. Rather than tweaking permissions for system directories, you simply tell npm to install global packages into a directory owned by your user account.

  • Find npm's current global directory:

npm config get prefix


You'll often see output like `/usr/local` or `/usr/bin`.

  
  - **Create a dedicated directory for global installations:** Pick a spot within your user's home directory. A widely accepted convention is `~/.npm-global`.
    ```bash
mkdir -p ~/.npm-global
  • Tell npm to use this new directory:

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

  
  - **Add the new directory to your PATH:** This crucial step ensures your shell can locate executables that npm installs globally. Open your shell's configuration file (`~/.bashrc`, `~/.zshrc`, or `~/.profile` are common) and insert the following line:
    ```bash
export PATH="~/.npm-global/bin:$PATH"

After adding the line, save the file. Then, either restart your terminal or apply the changes by sourcing the file:

```bash

source ~/.bashrc # Or ~/.zshrc, ~/.profile

  

#### Option 2: Fix Permissions for Existing Directories (Use with Caution)
Perhaps you prefer installing global packages in the default system location, or you're just facing a one-off permission problem. In such cases, you can opt to change the ownership of the problematic npm directories. While this approach is not ideal for long-term global package management, it can certainly resolve immediate issues.

**Warning:** Exercise extreme caution when using `sudo` and `chown`. Misusing these commands can potentially lead to system instability.

  - **Identify the affected directories:** You'll typically need to locate both the npm global installation directory and the npm cache directory.
    ```bash
npm config get prefix
npm config get cache

For example, the prefix might be /usr/local, and the cache could be ~/.npm or /Users/YOUR_USER/.npm.

  • Change ownership of the directories: Be sure to replace <NPM_PREFIX_DIR> and <NPM_CACHE_DIR> with the actual paths you found in the previous step. If you prefer, you can type out your username instead of using $(whoami).

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


Just so you know, `lib/node_modules`, `bin`, and `share` are the standard subdirectories for global packages and their executables.

  

#### Option 3: Fix Project-Specific Permissions
Is the `EACCES` error popping up within a particular Node.js project directory? For instance, when you're trying to run `npm install` or `npm start` locally? In such cases, it's highly probable that the project files or its `node_modules` directory have incorrect ownership.

  - **Navigate to your project directory:**
    ```bash
cd /path/to/your/project
  • Change ownership of the project directory: This command will recursively change the owner of all files and subdirectories inside your project to your current user.

sudo chown -R $(whoami) .


**Important:** Double-check that you are in the correct project directory before running this command!

  

#### Option 4: Clear npm Cache
Sometimes, a corrupted or permission-locked npm cache can be the culprit behind `EACCES` errors.

  - **Clear the npm cache:**
    ```bash
npm cache clean --force

This command completely removes all data from your npm cache. If the cache itself has permission issues, you might need to combine this step with changing the cache directory's ownership, as described in Option 2, before clearing it.

Verification

Once you've applied one or more of these fixes, it's essential to verify that the issue is truly resolved. Try repeating the action that previously failed.

  • For global package issues (Option 1 or 2): Attempt to install a new global package. A good test candidate is nodemon:

npm install -g nodemon


If `nodemon` installs successfully without any `EACCES` errors, your fix worked!

  
  - **For project-specific issues (Option 3 or 4):** Try installing project dependencies or running a project script.
    ```bash
cd /path/to/your/project
npm install
npm start # Or whatever script was failing

If these commands execute smoothly, without any permission errors, your project permissions are now likely correct.

Tips to Avoid Future EACCES Errors

  • Never use sudo npm install (unless absolutely necessary): Running npm commands with sudo is almost always a temporary workaround. It often creates more problems down the line by altering file ownership to root. Always configure npm properly using a method like Option 1 instead.
  • Embrace Node Version Manager (NVM) or Volta: Tools such as NVM (Node Version Manager) or Volta are game-changers. They allow you to install and manage multiple Node.js versions and their associated npm packages entirely within your user's home directory. This completely sidesteps the need to write to system directories for global packages, effectively eliminating EACCES errors for global installations.
  • Be mindful of shared development environments: If you're collaborating in a shared environment (e.g., a virtual machine or a team server), always ensure your user account has the appropriate permissions for all project directories.

Related Error Notes