Fix npm Permission Denied Error | Generated by AI
The error you’re encountering, EACCES: permission denied
, means your current user does not have write permissions to the /usr/local/lib/node_modules
directory. This is a common issue when installing global npm packages on Linux or macOS.
How to Fix This Issue
Option 1: Use sudo
(Not Recommended for Regular Use)
You can use sudo
to run the command as the root user, but this is not recommended for regular use, as it can lead to permission issues with npm packages later.
sudo npm install -g @anthropic-ai/claude-code
Option 2: Change npm’s Default Directory (Recommended)
The best practice is to configure npm to use a directory in your home folder where you have write permissions.
- Create a global npm directory in your home folder:
mkdir ~/.npm-global
- Configure npm to use this directory:
npm config set prefix '~/.npm-global'
- Add the new directory to your
PATH
: Open your shell configuration file (e.g.,~/.bashrc
,~/.zshrc
, or~/.profile
) and add this line:export PATH=~/.npm-global/bin:$PATH
Then, reload your shell:
source ~/.bashrc # or ~/.zshrc, ~/.profile
- Retry the installation:
npm install -g @anthropic-ai/claude-code
Option 3: Fix Permissions for /usr/local
(Advanced)
If you want to keep using /usr/local
, you can change its ownership to your user:
sudo chown -R $(whoami) /usr/local/lib/node_modules
Then retry the installation.
Recommendation: Use Option 2 to avoid permission issues in the future.