What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. It allows you to run JavaScript on the server side, enabling full-stack JavaScript development. Node.js comes with npm (Node Package Manager), which is the world's largest software registry.
- Fast: Built on Google Chrome's V8 JavaScript engine
- Scalable: Event-driven, non-blocking I/O model
- Popular: Large ecosystem with millions of packages
- Versatile: Build web servers, APIs, desktop apps, and more
Prerequisites
Before installing Node.js, make sure you have:
- Administrative access to your computer
- An active internet connection
- Basic familiarity with command line interface
Installing on Linux
There are several ways to install Node.js on Linux. We'll cover the most common methods.
Method 1: Using Package Manager (Recommended)
Ubuntu/Debian
# Update package index sudo apt update # Install Node.js and npm sudo apt install nodejs npm # Install build tools (optional but recommended) sudo apt install build-essential
CentOS/RHEL/Fedora
# For CentOS/RHEL 7 and older sudo yum install nodejs npm # For CentOS/RHEL 8+ and Fedora sudo dnf install nodejs npm
Method 2: Using NodeSource Repository
For the latest version, use the NodeSource repository:
# Download and run the setup script for Node.js 20.x curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - # Install Node.js sudo apt-get install -y nodejs
Method 3: Using Snap
sudo snap install node --classic
Installing on Windows
The easiest way to install Node.js on Windows is to download the installer from the official website.
Download the Installer
Visit nodejs.organd download the Windows Installer (.msi) for the LTS version.
Run the Installer
Double-click the downloaded .msi file and follow the installation wizard. Make sure to check the box that says "Automatically install the necessary tools."
Complete Installation
The installer will automatically add Node.js and npm to your PATH. You may need to restart your computer for the changes to take effect.
Alternative: Using Chocolatey
If you have Chocolatey installed, you can use it to install Node.js:
choco install nodejs
Alternative: Using Winget
winget install OpenJS.NodeJS
Installing on macOS
There are several ways to install Node.js on macOS. Here are the most popular methods:
Method 1: Using Homebrew (Recommended)
If you have Homebrew installed:
# Install Node.js and npm brew install node
Method 2: Download from Official Website
Visit nodejs.organd download the macOS Installer (.pkg) for the LTS version. Run the installer and follow the prompts.
Method 3: Using MacPorts
sudo port install nodejs18 +universal
Verify Installation
After installing Node.js, verify that it's working correctly by checking the versions:
# Check Node.js version node --version # Check npm version npm --version
You should see output similar to:
v20.10.0 10.2.3
Test Node.js
Create a simple test file to ensure Node.js is working:
echo "console.log('Hello, Node.js!');" > test.js node test.js
You should see "Hello, Node.js!" printed to the console.
Version Management
It's often useful to manage multiple versions of Node.js for different projects. Here are some popular version managers:
NVM (Node Version Manager)
NVM is the most popular Node.js version manager for Linux and macOS:
# Install NVM curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash # Reload your shell configuration source ~/.bashrc # Install the latest LTS version of Node.js nvm install --lts # Use a specific version nvm use 18.19.0 # List installed versions nvm list # Set default version nvm alias default 20.10.0
NVM for Windows
For Windows users, there's a separate NVM implementation:
- Download nvm-windows from GitHub
- Run the installer
- Use similar commands as above
Volta (Alternative)
Volta is a modern alternative that works on all platforms:
# Install Volta (Linux/macOS) curl https://get.volta.sh | bash # Install Node.js volta install node # Pin a specific version for a project volta pin node@18
Your First Node.js Project
Now that Node.js is installed, let's create a simple project to get you started.
Create Project Directory
Terminalmkdir my-first-node-app cd my-first-node-app
Initialize npm Project
Terminalnpm init -y
This creates a package.json file with default settings.
Create a Simple Server
Create an index.js file with a basic HTTP server:
index.jsconst http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/html' }); res.end('<h1>Hello, Node.js!</h1><p>Your server is running successfully.</p>'); }); const PORT = process.env.PORT || 3000; server.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}/`); });
Run Your Server
Terminalnode index.js
Open your browser and navigate to
http://localhost:3000
to see your server in action!Install a Package
Let's install Express.js, a popular web framework:
Terminalnpm install express
You've successfully installed Node.js and created your first project. You're now ready to start building JavaScript applications!
Conclusion
You've successfully installed Node.js and npm on your system. Node.js opens up a world of possibilities for JavaScript development, from web servers to desktop applications and command-line tools.
Next steps you might consider:
- Learn about npm packages and the Node.js ecosystem
- Explore popular frameworks like Express.js, Next.js, or NestJS
- Build REST APIs and web applications
- Learn about asynchronous programming with Promises and async/await
- Explore Node.js modules and the CommonJS/ES6 module systems
Useful npm Commands
# Install a package locally npm install package-name # Install a package globally npm install -g package-name # Install development dependencies npm install --save-dev package-name # Update packages npm update # List installed packages npm list # Run scripts defined in package.json npm run script-name # Check for outdated packages npm outdated