DEV Community

Cover image for Avoid `sudo n`
Henry Wu
Henry Wu

Posted on

Avoid `sudo n`

Note: It's acutally written in README.md, but if you are lazy like me who skips it, let me point it out for you.


If you found it troublesome to have to sudo every time to switch Node.js versions, then you are configuring n incorrectly (or, you are not configuring it at all).

I assume you have just got your new Mac and simply copy-and-paste installation commands from docs, so I assume you are using zsh and Homebrew.

Instead of using chown for /usr/local/n (and associated folders; which is safe to do), I choose to set N_PREFIX in .zshrc based on my preference to keep configs in one place (and with comments to remind me).

  1. Make .n folder under $HOME (your home directory) to store n installations:

    mkdir -p ~/.n/bin
    

  2. Add N_PREFIX to .zshrc:

    echo 'export N_PREFIX="$HOME/.n"' >> ~/.zshrc
    

  3. Add it to $PATH too:

    echo 'export PATH="$HOME/.n/bin:$PATH"' >> ~/.zshrc
    

  4. Load zsh config to current shell:

    source ~/.zshrc
    
  5. Install n using Homebrew:

    brew install n
    

  6. Install Node.js (LTS):

    n lts
    

🎉 Walla! Now you can n without sudo. No thank you, no thank you.

By the way, if you have an existing system that you would like to configure, you may run sudo n uninstall to remove /usr/local/n (and associated files). But do it first before setting N_PREFIX.


Cover image by Dim Gunger on Unsplash.

Top comments (0)