Npm Install G Permissions Error [Solved]

Immediate Fix: Change NPM’s Default Directory

The most reliable way to fix “EACCES” permissions errors without compromising system security is to tell npm to store global packages in a directory your user account already owns.

Follow these steps to move your global binaries to your home folder:

# 1. Create a directory for global installations
mkdir ~/.npm-global

# 2. Configure npm to use the new directory path
npm config set prefix '~/.npm-global'

# 3. Open or create your ~/.profile or ~/.bashrc file
nano ~/.profile

# 4. Add this line to the end of the file:
export PATH=~/.npm-global/bin:$PATH

# 5. Update your current shell
source ~/.profile

Now, you can run npm install -g <package> without using sudo or encountering permission denied errors.

Technical Explanation: Why EACCES Happens

By default, npm installs global packages in /usr/local/lib/node_modules. This directory is owned by the root user on most macOS and Linux systems.

When you run a global install as a standard user, the operating system blocks the process because it lacks “write” permissions for that specific system folder. This results in the EACCES error code.

Error Component Description
EACCES A permission error indicating the user cannot write to the directory.
Default Path /usr/local/lib/node_modules/
Recommended Owner The current logged-in user (not root).

Using sudo is a common “quick fix,” but it often leads to further permissions issues later when packages try to update or run lifecycle scripts. It is generally considered a bad practice in modern JavaScript development.

Terminal screen showing npm install g permissions error EACCES.

Alternative Methods

1. Use a Node Version Manager (nvm)

The professional standard for managing Node.js is using nvm. When you install Node via nvm, it automatically places everything in your user directory, bypassing system permissions entirely.

# Install nvm (example for macOS/Linux)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

# Install a Node version
nvm install node

# Global installs now work perfectly
npm install -g vercel

2. Manually Change Permissions (Not Recommended)

You can change the ownership of the system’s default npm folder to your current user. This is faster than moving the directory but can occasionally be overwritten by system updates.

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

This command grants your user account ownership over the folders npm uses for global installs, allowing you to bypass the EACCES restriction.