Node.js

Fix npm "EACCES: permission denied"

When npm install -g fails with a permission error. Install global packages in your own folder instead of using sudo.

Fill these in once
  1. Make a folder for global packages and tell npm to use it
    Terminal
    mkdir -p ~/.npm-global
    npm config set prefix ~/.npm-global

    Never fix this with sudo npm. Using nvm avoids the problem completely.

  2. Add that folder to your PATH
    Terminal
    echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.zshrc
    source ~/.zshrc
  3. Install again, without sudo
    Terminal
    npm install -g typescript

Check it worked

  1. Check where global packages go now
    Terminal
    npm config get prefix
Undo it
  1. Go back to npm's default folder
    Terminal
    npm config delete prefix
  1. Make a folder for global packages and tell npm to use it
    Terminal · Ubuntu / Debian
    mkdir -p ~/.npm-global
    npm config set prefix ~/.npm-global

    Never fix this with sudo npm. Using nvm avoids the problem completely.

  2. Add that folder to your PATH
    Terminal · Ubuntu / Debian
    echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
    source ~/.bashrc
  3. Install again, without sudo
    Terminal · Ubuntu / Debian
    npm install -g typescript

Check it worked

  1. Check where global packages go now
    Terminal · Ubuntu / Debian
    npm config get prefix
Undo it
  1. Go back to npm's default folder
    Terminal · Ubuntu / Debian
    npm config delete prefix

This error is a macOS and Linux problem. On Windows, npm's EPERM error usually means another program has the folder open: close VS Code and other terminals, then try again.