christogonus's avatar

remove the need for "sudo" on mac os terminal

This question is a little out of contest, but please help if you could.

Currently, to run most npm commands on my terminal, I require to add sudo in front of the command, else it will fail with permission issue.

I want to eliminate this asap.

My computer is the "Apple's MacBook Pro - M2 Chip"

Thanks.

0 likes
4 replies
LaryAI's avatar
Level 58

To remove the need for sudo when running npm commands on macOS, you can change the ownership of the npm directories to the current user. This way, you won't need to use sudo for installing global packages. Here's how you can do it:

  1. Open your terminal.

  2. Change the ownership of the global node_modules directory to your user. Replace YOUR_USERNAME with your actual username.

sudo chown -R $(whoami) /usr/local/lib/node_modules
  1. Change the ownership of the npm cache directory as well.
sudo chown -R $(whoami) ~/.npm
  1. If you have npx installed, you might also need to change the ownership of the npx cache directory.
sudo chown -R $(whoami) ~/.npx
  1. Optionally, if you have installed Node.js via Homebrew, you might need to change the ownership of the Homebrew's Cellar node directory.
sudo chown -R $(whoami) /usr/local/Cellar/node
  1. Lastly, change the ownership of the npm and npx binaries.
sudo chown -R $(whoami) /usr/local/bin/npm
sudo chown -R $(whoami) /usr/local/bin/npx

After performing these steps, you should be able to run npm commands without needing to prefix them with sudo.

Note: Be cautious when using sudo and chown commands, as they can affect system files and permissions. If you're not sure about your actions, it's always a good idea to seek help from someone who has more experience with Unix-based systems.

Alternative Solution:

Another approach is to configure npm to use a directory within your home directory for global installations. This way, you won't need to change any system-wide permissions.

  1. Make a directory for global installations:
mkdir "${HOME}/.npm-global"
  1. Configure npm to use the new directory:
npm config set prefix "${HOME}/.npm-global"
  1. Open or create a ~/.bash_profile or ~/.zshrc file and add the following line to it:
export PATH="${HOME}/.npm-global/bin:$PATH"
  1. Back on the command line, update your system variables:
source ~/.bash_profile
# Or if you're using zsh
source ~/.zshrc

Now, npm will install global packages in your home directory, and you won't need to use sudo for npm commands. Remember to restart your terminal or re-source your profile to ensure the changes take effect.

christogonus's avatar

@laryai 's answer was awesome, but it is either that I do not know how to implement the steps on my MAC or it doesnt work on the new chipset.

I still need help with this as laravel new project setup always turns red on the NPM package part. The image below could help simplify my question, in case I sis not structure it correctly.

https://prnt.sc/WIkY4DTwFdbj

Thanks.

christogonus's avatar

@Gogolashvili No I didn't find a way to fix it. I have accepted my fate and I simply include sudo at every npm command

Please or to participate in this conversation.