28

I was trying to install npm, and I got

npm ERR! Error: EACCES, Permission denied '/usr/local/lib/node_modules'
npm ERR! 
npm ERR! Please use 'sudo' or log in as root to run this command.
npm ERR! 
npm ERR!     sudo npm "install" "." "--force" "--global"
npm ERR! 
npm ERR! or set the 'unsafe-perm' config var to true.
npm ERR! 
npm ERR!     npm config set unsafe-perm true

How can log in as a root?

1
  • 4
    Do not login as root. ;-)
    – Chris Page
    Commented Apr 6, 2012 at 13:14

5 Answers 5

50

sudo -s is far easier than enabling the root user since it just starts up a shell with root permissions as a one step, on demand action. Not only is it fast, but it doesn't need to be reconfigured when you don't need the root user and doesn't expose the server to any more risk or vulnerability that adding a root user would entail.

Of course for npm you could just follow the directions and type:

sudo -H npm "install" "." "--force" "--global"

That still makes the npm command "root" and skips the log out step to get back out of root.

3
  • 8
    But better to sudo the one command rather than run under a shell as root
    – mmmmmm
    Commented Jun 19, 2011 at 20:52
  • 1
    Could you explain what sudo -s does in this context?
    – KatieK
    Commented Jul 31, 2013 at 19:51
  • 1
    @KatieK The command sudo -s runs a shell as root. The manual page has these details and much more. I will add some words since your question is a good one and many others might also wonder what it does.
    – bmike
    Commented Jul 31, 2013 at 20:22
13

I usually do it bit differently than how others described here. Lets imagine you have two users. standard user: john admin user: kevin

If john is logged in and wants to run as a root user(as kevin) here is how I do it.

su kevin

when asked enter kevin's password

sudo npm "install" "." "--force" "--global"

enter kevin's password again.

2
  • After you've already switched user to kevin, why do you need the sudo?
    – cm2
    Commented Jan 16, 2012 at 17:29
  • 1
    Because Kevin is not root, he is just allowed to become root.
    – Xvolks
    Commented Aug 29, 2018 at 15:36
5

You need to use the su (switch user) command to become root, but only root is allowed to become root, so you need to use sudo (super-user do). Therefore, type: sudo su.

[william@NCC-1701 ~]$ sudo su
Password:
[root@NCC-1701 /Users/william]$ 

If you haven't used root before, you should be made fully aware that one wrong character typed as root will delete your entire operating system, including your photos, emails and financial documents. Be extremely careful and always triple-read what you type before hitting enter. Otherwise, good luck!

5
  • 1
    been an AIX support technician for 3 years ... 12 years ago :) know some stuff but the simple stuff had vanished from my head :)
    – balexandre
    Commented May 18, 2011 at 18:51
  • I was going to snark my answer with friendly manual and google suggestions but quickly deleted it after reading your bio. Welcome to mac. I still can't bring myself to type vm_stat after years of AIX. Gotta love shell aliases ;-)
    – bmike
    Commented May 18, 2011 at 19:15
  • Why use sudo su instead of sudo -s?
    – Chris Page
    Commented Apr 6, 2012 at 13:13
  • 3
    All I see is NCC-1701 lol
    – Shane Hsu
    Commented Nov 18, 2013 at 10:52
  • @ShaneHsu good one!! i loved your comment...
    – Natsfan
    Commented Aug 29, 2018 at 16:14
2

I ran the same issue trying to install ionic, you only need to run the same command with sudo:

sudo npm install -g ionic cordova

The system will request the current user’s password. Provide it, and you will be able to install as superuser.

2

Other than what people have said you can run sudo <your-shell> this brings up the shell with root priveleges

sudo zsh
2
  • 1
    How does this differ from running sudo -s as explained in the accepted answer?
    – nohillside
    Commented Jan 9, 2021 at 9:48
  • @nohillside this might be too specific, but oh my zsh autocomplete doesn't autocomplete files in sudo -s zsh but works in sudo zsh
    – user397384
    Commented Jan 9, 2021 at 15:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .