NPM is a repository where there are around 1.3 million different packages that run with Node.js, which is the Javascript runtime environment. It includes a command-line tool that is used to download any package that you want to use in your application, or even make one yourself.
To install it, the default option is to download it from their official website and just follow the steps to set it up on your machine. That's the option we are going to be using for Windows. But for Mac and Linux there's a better alternative, and that's downloading it using Homebrew.
Homebrew is another package manager for Mac/Linux that will allow us to download multiple NPM/Node.js versions if required, as well as switch between them when needed. In my case, that's something that has come useful a lot recently at work and sometimes on personal projects to test different apps in different versions.
Which to download, Node.js LTS or Current?
LTS means Long-term support and Current would be the latest version released. Usually, there are multiple LTS versions because that way users are not in the need of switching versions through the years while still getting support in fixing bugs and issues that may arrive. Current versions only contain the latest features released; so, for most users, LTS is the way to go.
For Node.js, major version releases (v13, v14, v15, v16) enter Current status for six months. After those six months, if it was an odd-numbered version it will become "unsupported", meaning that the focus is not going to be on fixing issues for this version. But if it was an even-numbered version, it will become the active LTS version.
Installing Node.js and NPM with the default installer
To install Node.js and NPM on any operating system, visit their website and download the official installer.
Just hit any of the options, LTS or Current, to download it and follow the steps to install it on your computer. Once installed, restart your command line (CMD) and you should be able to run the commands to check the installed versions:
node --versionnpm --version
Installing Node.js and NPM on Mac or Linux using Homebrew
Installing Node/NPM with Homebrew is completely optional. You can always just follow the installation steps from the official website linked above instead. If you need or want a more advanced usage, you need to switch between versions and have multiple installed, or Homebrew would be useful with other tools you use as well (composer, gcc, etc.) then here's how to do install it and use it.
First, you should have the CLI Tools for XCode. This includes tools that will allow you to build things from source. If you don't have them you can install them in the App Store > Updates section or by running the following command:
sudo xcode-select --install
Then, just run the command to install Homebrew and follow the steps on the screen:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
As soon as it finishes, it should show you on screen the command to run in order to add Homebrew to your $PATH. Make sure you copy that command and run it.
Once you do that, you should be able to run this command to make sure everything got installed correctly:
brew doctor
If the brew command is not getting recognized, that means it wasn't correctly added to your PATH. You can try running these commands:
If you're on Mac OS:
echo 'PATH="/usr/local/bin:$PATH"' >> ~/.bash_profile
Also, run this if you're on Mac OS running Zsh:
echo 'PATH="/usr/local/bin:$PATH"' >> ~/.zshrc
And if you are using Linux, run the following commands:
test -d ~/.linuxbrew && eval "$(~/.linuxbrew/bin/brew shellenv)"test -d /home/linuxbrew/.linuxbrew && eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"test -r ~/.bash_profile && echo "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.bash_profileecho "eval \"\$($(brew --prefix)/bin/brew shellenv)\"" >> ~/.profile
Finally, to install Node.js (with NPM included) just run:
brew install node
If you are looking for a specific version, for example, Node 17, run the following command:
brew install node@17