[FIXED] Error 'npm ERR! code EACCES permission denied' in Linux
Quick Diagnostics
The error npm ERR! code EACCES permission denied (or EACCES: permission denied, access '/usr/local/lib/node_modules') occurs when trying to install global packages via npm install -g <package>. It happens because system Node directories are owned by root, preventing regular users from writing to them.
Quick Solution (Recommended): Change npm's default global directory to your home folder:
BASHmkdir ~/.npm-global npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc && source ~/.bashrc
🚀 Step-by-Step Fixes (Without sudo)
Method 1: Reconfigure NPM Default Global Directory
Running sudo npm creates security vulnerabilities and corrupts folder permissions for non-root users.
Create a global package folder in your HOME directory:
BASHmkdir -p ~/.npm-globalConfigure npm to use the new directory:
BASHnpm config set prefix '~/.npm-global'Append the new binary path to environment PATH: For Bash (
~/.bashrc):BASHecho 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrcFor Zsh (
~/.zshrc):BASHecho 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc source ~/.zshrcVerify global installation:
BASHnpm install -g typescript ts-node
Method 2: Use Node Version Manager (NVM)
NVM is the industry standard for managing Node.js environments. It installs Node versions inside the user's home directory, completely isolating permissions.
# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
# Reload terminal
source ~/.bashrc
# Install Node LTS
nvm install --lts
Prevention Advice
Recommended security practices: