How to install Node.js properly in 2023

How to install Node.js properly in 2023

Okay, so there's about a zillion guides on how to install Node.js on the internet, but most of them show you how to install Node.js using the official installer. There's nothing wrong with the official installer, but it will give you a headache later on, trust me.

First of all, it installs Node.js in a way that installation (and potentially usage) of global Node.js packages will require admin rights. So you will either run your commands with sudo, which is a security concern, or you'll tear out your hair trying to fix permission errors. Yes, it's fixable even with the official installer, but if you already know that, why are you reading a tutorial on Node.js installation? 😅

Installing with the official Node.js installer will also prevent you from easily switching between Node.js versions, which is something that you don't need until you do, and then it makes you cry, because uninstalling Node.js is a whole different beast.

Use a Node.js version manager

Please, avoid those horrible time sinks and install Node.js properly. But how to install Node.js properly, you ask? Using a node version manager. And no, I'm not talking about nvm, which has been my go-to manager for years of development, but lately it was slowing the startup of new terminals so much that I just could not bear it.

Luckily, it's 2023 now and we have fnm. Well, it's already been around for years, but now, after a year of using it, I feel confident to recommend it over nvm. It's been stellar all year. Very fast, very dependable. You should use it too.

How to install Node.js on macOS

It's very, very, VERY easy.

  1. Install Homebrew, if you don't already have it. Even my girlfriend loves it.

  2. Use Homebrew to install fnm.

  3. Use fnm to install Node.js

  4. ???

  5. Profit 💰 No sudo needed. No EACCES errors. Versions. 🌈🦄

So, how to do that?

Check or install Homebrew

Let's go. Time is of the essence. Open terminal and paste this command:

brew -v

Did it print Homebrew and some numbers? Great! That means you have Homebrew. You can move to fnm installation.

If you don't have Homebrew, paste this to your terminal:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

If you're concerned that I'm trying to hack you with an unknown script from the internet, you're concerned correctly, but I'm not trying to steal your crypto wallet. To feel safer, you can copy the command from the official Homebrew website.

Homebrew installation will take some time, but it's worth it. Don't forget to press enter a few times. Homebrew is the de facto standard package manager for macOS. It's hard to live without it once you get used to it.

At the end of the installation Homebrew asks you to run a few commands. Just copy them and paste them into your terminal, press enter, and it's done.

Install Fast Node Manager

Oh yes, fnm is Fast Node Manager, and it's fast. Like this tutorial, without 17 chapters of SEO fluff. But I digress. Paste this into your terminal:

brew install fnm

Yes, the power of brew 🍺 Incredible. Now comes the hardest part, because it's dependent on your shell.

If you know what a shell is

If you know what a shell is, great. If you're using bash or zsh, find your shell profile and add the following line to it, save, and restart terminal.

eval "$(fnm env --use-on-cd)"

More documentation can be found on Fast Node Manager's GitHub.

If you don't, no problem

If you don't know what a shell or a shell profile is, don't worry. It most likely means that you're also using bash or zsh. Go to your terminal and paste this:

ps -p $$

If you see /bin/bash in the output, you're using bash. If it's /bin/zsh you're using zsh. Now, based on the shell YOU are using, you either execute this command for bash:

cd ~
echo 'eval "$(fnm env --use-on-cd)"' >> .bashrc

Or this command for zsh:

cd ~
echo 'eval "$(fnm env --use-on-cd)"' >> .zshrc

Once it's done, restart the terminal and let's finally install Node.js.

Install Node.js

Have you restarted your terminal? If you did, you're ready to install Node.js with fnm. The version of Node.js to have in 2023 is 18. You can try 19 if you like to live dangerously.

fnm install 18
fnm use 18

And now, finally, check that Node.js is correctly installed.

node -v

Congratulations! 🎉 You've learned how to install Node.js the right way. Permission errors and version management will no longer plague your machine.