Installing the Node.js Runtime
JavaScript, traditionally, is a language we can use to run operations in the web browser and in web pages. Sometimes you'll want to write code that runs on your machine to automate tasks, build applications, or even write web servers. For this purpose we need a tool that can run JavaScript on its own outside of the web browser. The most popular non-web-browser runtime is Node.js.
If you'd like to install Node for yourself, there are a few options, but there's something you may want to keep in mind before making your choice. If you plan on working with Node.js often, or in existing projects, it can matter which version you're using. If the version might matter for you, consider a version manager!
If you're not sure, I'd recommend getting a version manager off-the-bat as you won't have to worry about it then. This is easy if you're on Linux, MacOS, or using Linux through Windows' WSL.
nvm: Node Version Manager (Recommended!)
If you're using Linux (think Ubuntu), MacOS, or WSL, you can easily run the official nvm installation script in your terminal:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
or
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
You'll find the most up-to-date instructions here. You can confirm if your installation was successful by running the following:
nvm --version
If the terminal result claims that this command doesn't exist, or isn't found, something went wrong or you'll need to carry out additional steps. If it prints the version number, you're good-to-go!
If the command wasn't found, try closing your terminal, and then opening it again. Run the version check again. Still nothing? Add the following to your terminal profile:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
If you're not sure where to put this, double-check which terminal emulator you're running. Consider the following profile paths:
- ~/.bash_profile
- ~/.bashrc
- ~/.zshrc
- ~/.profile
Once nvm is installed and ready-to-go you can easily download and switch Node version at any time! See the following commands:
Install a Specific Version of Node
# Installs Node version 21 and its associated npm package manager
nvm install 21
Use a Specific Version of Node
# Switches to Node version 21 for the rest of this terminal session
nvm use 21
Set the Default Version to Use
# Sets Node version 21 as the default any time you open a new terminal
nvm alias default 21
Windows or MacOS Installation
Don't need to be able to switch versions on-the-fly? You can go with the more traditional installer featured on their official world wide web site.
Visit the official web site's Prebuilt Installer web page: here. From there you'll be able to download the installation wizard for either Windows or Macintosh! Follow the on-screen instructions and you'll be in good shape.