I’ve been coding for around 4 weeks and keep hitting this annoying problem. Every time I try to install packages using npm, I get permission denied errors. The only way to make it work is by adding sudo before the command or changing directories constantly.
I already tried removing Node.js completely and installing it again, but the same issue keeps happening. Has anyone solved this permission problem permanently? What’s the best way to set up npm so it doesn’t require administrator privileges for basic package installations?
Any help would be great since this is really slowing down my development workflow.
Had this same headache when I switched to Mac development about 6 months ago. What solved it permanently for me was fixing the ownership of the npm directories rather than working around it. You can run sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share} to take ownership of the global npm folders. After that, regular npm install commands work without any sudo nonsense. The key is making sure your user account owns these directories instead of root. I’ve been using this setup daily since then and haven’t had a single permission issue. Much simpler than switching to version managers if you just want to fix your current setup.
I ran into this exact same issue when I started developing on macOS last year. The root cause is usually that npm was installed with elevated permissions, so the global modules directory is owned by root instead of your user account. What fixed it permanently for me was using a Node version manager like nvm instead of the system installation. After installing nvm, I reinstalled Node through it and the permission issues disappeared completely. The advantage is that nvm installs everything under your home directory with proper user ownership. You can install nvm with the curl command from their GitHub page, then use nvm install node to get the latest version. This approach is cleaner than changing prefixes and you won’t need sudo for global packages anymore.
try changing your npm prefix to a dir you own instead of the global one. run npm config set prefix ~/.npm and add ~/.npm/bin to your PATH in your ~/.bash_profile or ~/.zshrc. this worked for me after weeks of frustration!