Mac下使用npm安装Cordova与Ionic时遭遇权限错误求助
Hey there! Let's work through this problem together—you're totally right that this is a permission issue, and we've got a couple of solid ways to fix it.
What's Causing the Error?
The EACCES error happens because your regular user account doesn't have write access to the /usr/local/lib/node_modules directory. npm needs this access to install global packages like Cordova and Ionic, so it's blocked by macOS's security settings.
Option 1: Use a User-Specific Directory for Global npm Packages (Recommended)
This is the safer approach because it avoids messing with system-level directories or using sudo (which can cause permission headaches later):
- First, create a folder in your user directory to store global npm packages:
mkdir ~/.npm-global - Tell npm to use this new folder as its global prefix:
npm config set prefix '~/.npm-global' - Update your shell configuration to recognize the new package location:
- If you use Bash, open
~/.bash_profilein a text editor and add this line:export PATH=~/.npm-global/bin:$PATH - If you use Zsh (the default on newer Macs), edit
~/.zshrcinstead and add the same line.
- If you use Bash, open
- Make the changes take effect immediately:
source ~/.bash_profile # For Bash users # OR source ~/.zshrc # For Zsh users - Now you can install Cordova and Ionic without permission issues:
npm install -g cordova ionic
Option 2: Change Ownership of the System Directory
If you prefer to modify the existing system directory instead, you'll need to use sudo—but first, note that the password you need to enter is your Mac's administrator password (the same one you use to log into your computer). When you type it in Terminal, you won't see any characters appear (this is normal security behavior)—just type it and press Enter.
Run this command to take ownership of the node_modules directory:
sudo chown -R $USER:$GROUP /usr/local/lib/node_modules
Once that's done, run your original install command again:
npm install -g cordova ionic
Quick Note About the sudo Password
If your password isn't working, double-check that you're using your current user's login password. If that still fails, go to System Settings > Users & Groups to confirm your account has administrator privileges.
内容的提问来源于stack exchange,提问作者shirosHeroics

