Access denied errors in npm typically stem from file permission issues when installing global packages or writing to node_modules. Use nvm for Node.js management, configure a user-writable npm prefix, or fix directory ownership.
This error indicates npm cannot read from or write to a directory it needs access to. The most common scenario is trying to install global packages without proper permissions to /usr/local/lib/node_modules. On Unix-like systems, the global npm directory is typically owned by root, while npm runs as your user. On Windows, similar issues occur when npm tries to write to protected directories or when antivirus software blocks file operations. The error can also occur with local installations if your project's node_modules directory has incorrect ownership—often from previously running npm with sudo.
The recommended long-term solution is using nvm:
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# Open new terminal, then:
nvm install --lts
nvm use --ltsnvm keeps everything in ~/.nvm, avoiding permission issues entirely.
If the error is in your project's node_modules:
# Change ownership to your user
sudo chown -R $(whoami) node_modules
# Or remove and reinstall
rm -rf node_modules package-lock.json
npm installSet up npm to install global packages in your home folder:
npm config set prefix ~/.local
echo 'export PATH=~/.local/bin:$PATH' >> ~/.bashrc
source ~/.bashrcThis avoids needing to modify system directories.
If errors reference the npm cache:
sudo chown -R $(whoami) ~/.npm
npm cache clean --forceOn Windows, access denied often comes from:
1. Antivirus: Temporarily disable real-time scanning and retry
2. Open editors: Close VSCode, WebStorm, or other IDEs that might lock files
3. Terminal: Run Command Prompt or PowerShell as Administrator (for one-time fixes only)
Clear npm cache after:
npm cache clean --forceIn WSL, permission issues are common when working in /mnt/c/:
# Move project to WSL filesystem
mv /mnt/c/project ~/project
cd ~/project
rm -rf node_modules
npm installThe Linux filesystem in WSL handles permissions correctly, unlike mounted Windows drives.
Why sudo npm is dangerous:
1. Security risk: npm runs package scripts during installation. Running as root means those scripts have root access.
2. Permission cascade: Creates root-owned files that require sudo for all future operations.
3. Breaks user installs: Mixing sudo and non-sudo npm corrupts permissions.
If you've already used sudo npm, the fix is to:
1. Remove node_modules: sudo rm -rf node_modules
2. Fix global directory ownership or switch to nvm
3. Never use sudo with npm again
For CI/CD:
If you see access denied in CI pipelines:
- Ensure the CI user has write access to the workspace
- Don't run npm as root in Docker
- Use npm ci instead of npm install for cleaner installs
- Check that previous pipeline steps didn't change permissions
npm ERR! code ENOAUDIT npm ERR! Audit endpoint not supported
How to fix "npm ERR! code ENOAUDIT - Audit endpoint not supported"
npm ERR! code EBADDEVENGINES npm ERR! devEngines.runtime incompatible with current node version
How to fix "npm ERR! code EBADDEVENGINES - devEngines.runtime incompatible with current node version"
npm ERR! code ETOOMANYARGS npm ERR! Too many arguments
How to fix "npm ERR! code ETOOMANYARGS - Too many arguments"
npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name: tag names cannot contain spaces
How to fix "npm ERR! code EINVALIDTAGNAME - tag names cannot contain spaces"
npm ERR! code E400 npm ERR! 400 Bad Request
How to fix "npm ERR! code E400 - 400 Bad Request" error