Fixing Global npm Install Permissions on macOS
> PS: This is also a note to myself in case I need it again in future
I recently switched to macOS and tried installing some global npm packages like pnpm
using:
npm install -g pnpm
But I got a permissions error saying it couldn’t create a folder in /usr/local/lib/...
. This happens because macOS doesn’t allow normal users to write to that system directory by default.
Here’s how I fixed it:
Step 1: Create a folder to store global npm packages
mkdir ~/.npm-global
Why: This creates a new directory in your home folder where you’ll install global npm packages safely, without needing admin rights.
Step 2: Tell npm to use that folder
npm config set prefix '~/.npm-global'
Why: This changes npm’s settings so it knows to install global packages into the new folder you just created instead of the system one.
Step 3: Add that folder to your system PATH
Open your terminal and run:
nano ~/.zshrc
Then add this line at the bottom of the file:
export PATH="$HOME/.npm-global/bin:$PATH"
Why: This tells your system where to find the npm packages you install globally, so you can run them like any other command (e.g., pnpm
or eslint
).
Step 4: Apply the changes
source ~/.zshrc
Why: This reloads your terminal settings so the PATH change takes effect immediately.
Step 5: Install the global package again
npm install -g pnpm
Why: Now that everything is set up, this should install without any permission error.
Step 6: Verify it works
pnpm -v
Why: This checks if pnpm
was installed correctly and is available to run.
What If You Installed pnpm But It Still Says “command not found”?
After installing pnpm, I ran this in my project:
pnpm install
But the terminal said:
zsh: command not found: pnpm
That means macOS can’t find the pnpm command yet. Here’s how I fixed it:
Step A: Check where pnpm was installed
npm config get prefix
If it shows something like /Users//.npm-global, then run:
ls ~/.npm-global/bin
If pnpm is listed, we just need to make sure the terminal can see it.
Step B: Add it to your PATH (if not already done)
Open your terminal config again:
nano ~/.zshrc
Make sure this line exists at the bottom:
export PATH="$HOME/.npm-global/bin:$PATH"
Then save and close the file.
Step C: Reload your terminal settings
source ~/.zshrc
Now try:
pnpm -v
If it works, then run pnpm install in your project folder again. Everything should now work smoothly.