Change NPM Global Package Location on macOS to Avoid Permission Errors

June 24, 2025
157 views

Problem

When installing global NPM packages on macOS, you might encounter permission errors like:

npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'

This forces you to use sudo npm install -g package-name.

Solution

Configure NPM to use a custom directory in your home folder where you have full permissions. This eliminates permission errors and the need for sudo.

Setup Steps

1. Create Global Packages Directory

mkdir ~/.npm-global

2. Configure NPM Prefix

npm config set prefix '~/.npm-global'

Verify the configuration:

npm config get prefix
# Should output: /Users/your_username/.npm-global

3. Update PATH Environment Variable

For Zsh (default on macOS Catalina+):

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc

For Bash:

echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bash_profile

4. Reload Shell Configuration

# For Zsh
source ~/.zshrc

# For Bash
source ~/.bash_profile

5. Test Installation

Install a package globally without sudo:

npm install -g jshint

Verify it works:

jshint --version

Troubleshooting

Command not found after installation:

  • Restart your terminal
  • Check PATH: echo $PATH should include ~/.npm-global/bin

Still getting permission errors:

  • Verify prefix: npm config get prefix
  • Check directory ownership: ls -la ~/.npm-global

Alternative: Use Node Version Manager (NVM)

For a more comprehensive solution, consider using NVM which manages both Node.js versions and avoids permission issues entirely:

# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# Install latest Node.js
nvm install node

This approach eliminates permission issues and allows easy Node.js version switching.


0 Comments


Leave a Comment

Share your questions, thoughts and ideas while maintaining a considerate tone towards others, thank you.

All fields are required - your email address will not be published.