Node.js is a powerful, open-source JavaScript runtime environment that allows developers to execute JavaScript code outside of web browsers. It's widely used for building server-side applications, real-time web applications, and command-line tools. This tutorial will guide you through the process of installing Node.js on various operating systems, including Windows, macOS, and Linux.
Before proceeding with the installation, ensure that your system meets the following requirements:
Node.js can be installed in several ways, including using the official installer, package managers, or version management tools. Below are the steps for each method.
The easiest way to install Node.js is by downloading the official installer from the Node.js website.
Package managers are software tools that help automate the installation and management of software packages.
Homebrew is a popular package manager for macOS. If you don't have Homebrew installed, follow these steps:
Install Homebrew:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Node.js using Homebrew:
brew install node
Most Linux distributions come with package managers like apt for Ubuntu, yum for CentOS, or dnf for Fedora.
Using apt (Ubuntu):
sudo apt update
sudo apt install nodejs npm
Using yum (CentOS):
sudo yum install epel-release
sudo yum install nodejs npm
Using dnf (Fedora):
sudo dnf module enable nodejs:14
sudo dnf install nodejs
Version management tools like nvm (Node Version Manager) allow you to easily switch between different versions of Node.js.
Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Load nvm:
Add the following lines to your shell configuration file (e.g., .bashrc, .zshrc):
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" # This loads nvm
Install Node.js using nvm:
nvm install node # Installs the latest version
nvm install --lts # Installs the LTS version
For Windows, you can use nvm-windows, a port of nvm for Windows.
Download and Install nvm-windows:
Install Node.js using nvm-windows:
nvm install latest # Installs the latest version
nvm install lts # Installs the LTS version
After installation, verify that Node.js and npm (Node Package Manager) are correctly installed by running the following commands in your terminal or command prompt:
node -v
npm -v
These commands should display the installed versions of Node.js and npm, respectively.
On Windows, you might need to set up environment variables to ensure that Node.js is accessible from any location in your command prompt.
Open System Properties:
This PC or Computer on the desktop or in File Explorer.Properties.Advanced system settings.Environment Variables:
Environment Variables button.System variables, find and select the Path variable, then click Edit.C:\Program Files\nodejs).To enhance your development experience, consider installing additional tools like:
Download and Install:
Install Extensions:
Extensions (Ctrl+Shift+X) and search for popular extensions like "ESLint", "Prettier", and "Node.js Extension Pack".Download and Install:
Configure Git:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
nvm to manage different versions of Node.js, especially in development environments.Installing Node.js is the first step in setting up a development environment for building server-side applications. By following this comprehensive guide, you should have Node.js installed and ready to use on your system. Whether you're using the official installer, package managers, or version management tools, the process is straightforward and can be tailored to fit your specific needs.
Happy coding!