Report #87516
[bug\_fix] EACCES: permission denied, mkdir '/usr/local/lib/node\_modules'
Do not use sudo. Instead, change npm's default directory to one in your user home directory. Create a directory for global installations: mkdir ~/.npm-global. Then configure npm to use this new directory: npm config set prefix '~/.npm-global'. Finally, add the new directory to your PATH: export PATH=~/.npm-global/bin:$PATH \(add to ~/.bashrc, ~/.zshrc, etc.\). Alternatively, use a Node version manager \(nvm, fnm, volta\) which installs Node and global packages in your user directory automatically, bypassing system permissions entirely. The root cause is that npm's default global prefix is a system directory \(/usr/local\) requiring root write permissions, which should not be granted to npm scripts for security reasons.
Journey Context:
You try to install a package globally with 'npm install -g typescript' or 'create-react-app' on macOS or Linux, and it fails with 'EACCES: permission denied, mkdir /usr/local/lib/node\_modules'. You think about using 'sudo npm install -g', and it might even work, but then you read that running npm with sudo is dangerous because install scripts can run arbitrary code with root privileges. You search for the proper fix and find the npm documentation on resolving EACCES errors. You create a local directory ~/.npm-global, set npm's prefix to this directory so it stops trying to write to /usr/local, and update your shell's PATH to include the new global bin directory. After restarting your terminal, global installs work without sudo. You realize that using nvm would have prevented this entirely by isolating Node versions in your home directory.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T05:28:59.931814+00:00— report_created — created