Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, enabling developers to execute JavaScript code outside of a web browser. This guide walks through the complete installation and configuration process across different operating systems.
Downloading the Installation Package
Navigate to the official Node.js website at https://nodejs.org. The download page presents two distinct versions: the Long Term Support (LTS) release and the Current release. The LTS version receives extended support and updates, making it the recommended choice for production environments and most development scenarios. The Current version includes the latest features but may harbor instabilities.
Select the appropriate installer for your operating system. The website automatically detects your platform and suggests the correct package, though you can manually choose between Windows (.msi), macOS (.pkg), or Linux distributions.
Running the Installation
Windows
Execute the downloaded .msi file by double-clicking it. The installation wizard guides you through the process, including options to add Node.js to your system PATH variable automatically. Accept the default settings unless you have specific requirements.
macOS
Open the downloaded .pkg file and follow the installation prompts. The installer copies the necessary files to /usr/local/bin/node and configures your environment appropriately.
Linux (Debian/Ubuntu)
For Debian-based distributions, you can install Node.js through the package manager:
sudo apt update
sudo apt install nodejs npm
Alternatively, version managers like nvm or n provide greater flexibility when working with multiple Node.js versions simultaneously. These tools allow switching between versions without reinstalling and prevent permission-related issues.
Verifying the Installation
After installation, open your terminal or command prompt and confirm that Node.js was installed correctly:
node --version
This command displays the installed Node.js version number. Verify npm installation as well:
npm --version
Both commands should return version information without errors. If you encounter "command not found" errors, restart your terminal session or manually add the installation directory to your system PATH.
Environment Configuration
Linux and macOS Setup
When using version managers like nvm, you may need to configure your shell initialization file. Edit ~/.bashrc, ~/.zshrc, or ~/.profile depending on your shell:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
Apply the changes by sourcing your configuration file:
source ~/.bashrc
Managing Packages with npm
The Node Package Manager (npm) serves as the default package ecosystem for Node.js, providing access to thousands of reusable libraries and tools. Install packages using the install command:
npm install express
To save the package as a project dependency automatically, include the --save flag:
npm install express --save
The --save-dev flag installs packages as development dependencies, typically used for testing frameworks and build tools.
Initializing a New Project
Create a dedicated directory for your project and initialize it with npm:
mkdir my-project
cd my-project
npm init -y
This generates a package.json file containing metadata about your project, including name, version, description, and dependency declaartions. The -y flag accepts default values; omit it to customize each property manually.
Creating Your First HTTP Server
Write a simple HTTP server to verify your Node.js setup. Create a file named server.js in your project directory:
const http = require('http');
const PORT = 8080;
const server = http.createServer((request, response) => {
response.writeHead(200, { 'Content-Type': 'text/html' });
response.end('<html><body><h1>Welcome to Node.js</h1></body></html>');
});
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`);
});
This example demonstrates basic server creation using the built-in http module. The server listens on port 8080 and responds with a simple HTML page.
Launching the Application
Execute your server script using the Node.js runtime:
node server.js
Once running, open your web browser and navigate to http://localhost:8080. You should see the welcome message rendered in the browser window. Press Ctrl+C in your terminal to stop the server.
Global Package Installation
Global packages install to a system-wide location and become available from any directory. Use the -g flag for global installation:
npm install -g typescript
Global installations require elevated permissions on some systems. Consider using a version manager to avoid permission issues without requiring sudo.
Working with package-lock.json
Node.js generates a package-lock.json file that records exact versions of all installed dependencies. This file ensures consistent installations across different environments by locking dependencies to specific versions. Commit this file to version control to maintain reproducible builds.
Updating Node.js Versions
When using nvm, list avialable versions and switch between them:
nvm list
nvm install 20
nvm use 20
This approach enables testing your application against different Node.js versions and simplifies upgrades without system-wide modifications.