How to Fix 'npm install' Fails with EACCES Error on macOS/Linux
Problem Explanation
When attempting to install Node.js packages using npm install on macOS or Linux systems, you might encounter an EACCES error. This error indicates "Permission denied," meaning the npm command is trying to write files or create directories in a location where your current user account lacks the necessary write privileges. This issue commonly arises when installing global packages (e.g., npm install -g <package-name>), but can also affect local project dependencies if the npm cache or project directory permissions are misconfigured.
The typical output you'll see in your terminal will include "EACCES: permission denied," often pointing to a specific path like /usr/local/lib/node_modules or within your user's .npm directory. Here's an example of such an error message:
npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
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! 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 appear to be the correct owner, please review any system documentation for
npm ERR! shared file systems, or file permissions.
Why It Happens
The EACCES error primarily occurs because Node.js and npm were installed in a way that granted root ownership to system-wide directories where npm attempts to write. This often happens under a few scenarios:
- Node.js/npm installed with
sudo: If Node.js ornpmwere initially installed or updated usingsudo(e.g.,sudo apt install nodejsorsudo npm install -g npm), the globalnode_modulesdirectory andnpm's cache directories might have been created or modified with root ownership. Subsequentnpm installcommands run withoutsudowill fail because your regular user lacks permission to write to those root-owned directories. - System-wide installation: Using a system package manager (like
apton Debian/Ubuntu,yum/dnfon Fedora/RHEL, or a direct macOS installer without a version manager) often places Node.js andnpmfiles in system locations (/usr/local,/usr/bin, etc.) that typically require root privileges for modification. - Corrupted permissions: Less common, but permissions in your user's
~/.npmcache directory or a specific project'snode_modulesdirectory might become incorrect due to various system events, leading to permission issues even for local installations.
The core issue is a mismatch between the user executing npm install and the owner of the target installation directories. Solving this requires ensuring your user has proper write access without resorting to sudo for every npm command, which is a security risk and perpetuates the problem.
Step-by-Step Solution
The most robust and recommended solution is to use a Node Version Manager (NVM). This completely sidesteps permission issues by installing Node.js and npm within your user's home directory. If NVM is not an option for specific reasons, alternative methods involving permission adjustments are also provided.
Step 1: Install and Configure Node Version Manager (NVM) (Recommended)
NVM allows you to install and manage multiple Node.js versions on your system without requiring root access. It places Node.js and npm in your user's home directory, /home/<user>/.nvm (Linux) or ~/.nvm (macOS), which you always have write access to.
-
Remove existing Node.js/npm installations: Before installing NVM, it's highly recommended to remove any existing system-wide Node.js and
npminstallations that might be causing conflicts.- macOS (if installed via Homebrew):
brew uninstall node brew cleanup - macOS (if installed via official installer): You may need to manually remove files from
/usr/local/bin,/usr/local/lib,/usr/local/include, etc. Use caution.# List files that might be Node.js related ls -la /usr/local/bin | grep "node" ls -la /usr/local/bin | grep "npm" # Then remove them if they exist and are related to Node.js/npm (use with extreme caution) # sudo rm -rf /usr/local/{lib/node_modules,bin/node,bin/npm,bin/npx} - Linux (if installed via
apt/yum/dnf):sudo apt remove nodejs npm # For Debian/Ubuntu sudo yum remove nodejs npm # For Fedora/RHEL/CentOS sudo dnf remove nodejs npm # For modern Fedora - Clear
npmcache:
You may neednpm cache clean --forcesudofor this if permissions are already broken.
- macOS (if installed via Homebrew):
-
Install NVM: Open your terminal and run the official NVM installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash(Verify the latest version on NVM's GitHub page if v0.39.7 is outdated.)
-
Verify NVM installation and load it: After installation, close and reopen your terminal, or source your shell's profile:
source ~/.bashrc # Or ~/.zshrc, ~/.profile, depending on your shell nvm --versionIf it outputs a version number, NVM is installed correctly.
-
Install Node.js using NVM: Install the latest stable version of Node.js:
nvm install node # Installs the latest stable version nvm use node nvm alias default node # Set this version as default for new shellsYou can also install specific versions, e.g.,
nvm install 18. -
Verify Node.js and npm:
node -v npm -vBoth should output version numbers. Now,
npm installoperations should work withoutEACCESerrors.
Step 2: Fix npm's Default Directory (Alternative to NVM)
If you cannot use NVM, you can configure npm to install global packages into a directory within your home folder, which you have full control over.
-
Create a directory for global
npmpackages:mkdir ~/.npm-global -
Configure
npmto use this new directory:npm config set prefix '~/.npm-global' -
Add the new
npmpath to your shell'sPATHenvironment variable: Open your shell configuration file (e.g.,~/.bashrc,~/.zshrc, or~/.profile) with a text editor:nano ~/.bashrcAdd the following line at the end of the file:
export PATH="$PATH:~/.npm-global/bin"Save the file and exit the editor.
-
Apply the changes: Close and reopen your terminal, or run:
source ~/.bashrc # Or ~/.zshrc, ~/.profile -
Test the fix: Try installing a global package:
npm install -g yarnThis should now install Yarn into
~/.npm-global/binwithout permission errors.
Step 3: Change Ownership of npm's Global Directory (Use with Caution)
This method directly changes the ownership of the system-wide global npm directories to your user. While effective, it's less preferred than NVM or changing the prefix, as it modifies system directories and can lead to issues if Node/npm are reinstalled by a system process.
-
Find your
npmglobal directory:npm root -gThis command will typically output a path like
/usr/local/lib/node_modules. Let's assume the output is/usr/local/lib/node_modules. -
Change ownership of the directory: Replace
<YOUR_USERNAME>with your actual username and/usr/local/lib/node_moduleswith the path found in the previous step.sudo chown -R $(whoami) $(npm root -g)This command recursively changes the owner of the
node_modulesdirectory (and everything inside it) to your current user. -
Change ownership of
npm's cache directory:sudo chown -R $(whoami) ~/.npm -
Clear the
npmcache:npm cache clean --force -
Test the fix:
npm install -g @vue/cli # Example global package
Step 4: Clear npm Cache
Sometimes, a corrupted npm cache can lead to unexpected permission issues. Clearing it can resolve problems.
- Clear the cache:
If you still facenpm cache clean --forceEACCESwhen clearing, you might need to usesudoonce for this specific command if the cache directory permissions are severely broken:
After clearing withsudo npm cache clean --forcesudo, trynpm installagain. If successful, use the method from Step 1 or 2 to prevent future issues.
Step 5: Verify Node.js and npm Installation
After applying any of the above solutions, always verify that Node.js and npm are functioning correctly.
-
Check versions:
node -v npm -vThese should output the installed version numbers without errors.
-
Test a global installation:
npm install -g nodemonIf this runs successfully without
EACCESerrors, your problem is resolved.
Common Mistakes
- Using
sudo npm installas a regular fix: This is the most common mistake. Whilesudotemporarily bypasses the permission error, it does so by runningnpmas the root user. This creates or modifies files with root ownership, perpetuating theEACCESproblem for futurenpmcommands run by your regular user. It's a temporary workaround that makes the underlying problem worse. - Incorrectly removing Node.js/npm: Rushing to remove Node.js without understanding how it was installed can leave remnants that cause conflicts with new installations, especially when switching to version managers.
- Applying
chownto the wrong directory: Usingsudo chown -Ron an incorrect or overly broad directory can destabilize your system. Always target specificnpmor Node.js directories as identified bynpm root -g. - Not sourcing shell profiles: After modifying
~/.bashrcor~/.zshrc, users sometimes forget to runsource ~/.bashrc(or equivalent) or reopen their terminal, leading to the newPATHor environment variables not taking effect.
Prevention Tips
To prevent EACCES errors from recurring and ensure a smooth development workflow:
- Always use a Node Version Manager (NVM): This is the single most effective prevention strategy. NVM installs Node.js and
npminto your user's home directory, ensuring you always have the necessary permissions. It also allows you to easily switch between Node.js versions for different projects, which is a common requirement in development. - Avoid
sudo npm installfor global packages: Unless you have a very specific, well-understood reason, never usesudowhen installing globalnpmpackages. If you find yourself needingsudo, it's a strong indicator that your Node.js/npm setup has permission issues that need to be addressed at a fundamental level (e.g., using NVM). - Regularly clear your
npmcache: While not directly related toEACCES, a cleannpmcache (npm cache clean --force) helps prevent other installation issues and ensures a fresh slate, especially after resolving permission problems. - Understand your
PATH: Be aware of where yournodeandnpmexecutables are located in your system'sPATH. If you have multiple installations or are using a version manager, ensure the correct version is prioritized in yourPATHto avoid unexpected behavior. - Check system permissions: If you're working on a shared system or a non-standard setup, occasionally verify the permissions of key directories (
/usr/local/lib/node_modules,~/.npm) to ensure they align with your user's access rights.