Angular CLI安装报错errno -13(EACCES权限问题),求解决方法
Hey Wei, that EACCES error is a super common hiccup when installing global npm packages—basically, your user account doesn’t have write permissions to the default global node_modules folder at /usr/local/lib/node_modules. Let’s walk through a few safe, practical fixes:
Option 1: Temporary Sudo Install (Quickest Short-Term Fix)
If you just need to get Angular CLI installed right away, you can run the command with elevated privileges:
sudo npm install -g @angular/cli
Just enter your system password when prompted. Note: Packages installed this way will require sudo for future updates, and might cause file permission quirks down the line.
Option 2: Change Ownership of npm’s Global Directories (Recommended)
This adjusts permissions so your regular user owns the directories npm needs to write to, eliminating the need for sudo entirely:
- First, confirm npm’s global prefix directory:
This should returnnpm config get prefix/usr/localfor your setup. - Update the owner of the relevant subdirectories to your user:
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share} - Now you can run the original install command without issues:
npm install -g @angular/cli
Option 3: Use a Custom Global npm Directory (Most Secure)
If you don’t want to modify system directory permissions, set up a dedicated global folder in your user directory:
- Create the custom directory:
mkdir ~/.npm-global - Configure npm to use this directory as its global prefix:
npm config set prefix '~/.npm-global' - Add the custom directory’s
binfolder to your shell’sPATH(so your system can find globally installed tools likeng):- Open your shell config file (e.g.,
~/.bashrcfor Bash,~/.zshrcfor Zsh):nano ~/.bashrc - Add this line at the end:
export PATH=~/.npm-global/bin:$PATH - Save and exit, then refresh the config:
source ~/.bashrc
- Open your shell config file (e.g.,
- Now install Angular CLI normally:
npm install -g @angular/cli
Quick Side Note
Your Node.js version (v8.11.1) is quite outdated—newer versions of Angular CLI may not support it. If you run into additional errors after fixing permissions, consider upgrading Node.js to a supported LTS version (like 14.x or 16.x) to ensure full compatibility.
内容的提问来源于stack exchange,提问作者Wei

