Home » [FIXED] Error 'npm ERR! code EACCES permission denied' in Linux
Web & Code

[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:

BASH
mkdir ~/.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.

  1. Create a global package folder in your HOME directory:

    BASH
    mkdir -p ~/.npm-global
    
  2. Configure npm to use the new directory:

    BASH
    npm config set prefix '~/.npm-global'
    
  3. Append the new binary path to environment PATH: For Bash (~/.bashrc):

    BASH
    echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
    source ~/.bashrc
    

    For Zsh (~/.zshrc):

    BASH
    echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
    source ~/.zshrc
    
  4. Verify global installation:

    BASH
    npm 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.

BASH
# 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: