How to Install Nodejs
How to Install Node.js Node.js has become one of the most essential technologies in modern web development. Built on Chrome’s V8 JavaScript engine, Node.js allows developers to run JavaScript on the server side, enabling the creation of fast, scalable network applications. Whether you’re building a REST API, a real-time chat application, a microservice, or a full-stack JavaScript application, Node
How to Install Node.js
Node.js has become one of the most essential technologies in modern web development. Built on Chrome’s V8 JavaScript engine, Node.js allows developers to run JavaScript on the server side, enabling the creation of fast, scalable network applications. Whether you’re building a REST API, a real-time chat application, a microservice, or a full-stack JavaScript application, Node.js provides the foundation to do so efficiently. Installing Node.js correctly is the first critical step toward unlocking its full potential. This tutorial provides a comprehensive, step-by-step guide to installing Node.js on Windows, macOS, and Linux systems, along with best practices, recommended tools, real-world examples, and answers to frequently asked questions. By the end of this guide, you’ll have a solid, production-ready Node.js environment configured and ready for development.
Step-by-Step Guide
Installing Node.js on Windows
Installing Node.js on Windows is straightforward and can be completed in under five minutes using the official installer. Follow these steps carefully to ensure a clean and functional installation.
- Visit the official Node.js website at https://nodejs.org. The homepage displays two versions: LTS (Long-Term Support) and Current. For most users, especially beginners and production environments, select the LTS version. It is more stable and receives long-term security updates.
- Click the green “Install” button to download the Windows Installer (.msi file). Do not download the .exe or .zip files unless you have specific requirements for portable installations or enterprise deployment.
- Once the download is complete, locate the .msi file in your Downloads folder and double-click to launch the installer.
- The Node.js Setup Wizard will open. Click “Next” to proceed through the welcome screen.
- Read and accept the license agreement, then click “Next” again.
- Choose the installation location. The default path (usually C:\Program Files\nodejs\) is recommended. Click “Next” to continue.
- Select the components to install. Ensure that “Node.js runtime,” “npm package manager,” and “Add to PATH” are all checked. The “Add to PATH” option is critical—it allows you to run Node.js and npm commands from any directory in your command prompt or PowerShell.
- Click “Next,” then “Install.” The installer will now copy files and configure your system. This may take a minute.
- Once installation is complete, click “Finish.”
- Open a new Command Prompt or PowerShell window (important: restart any open terminals to refresh the PATH variable).
- Type the following command and press Enter:
node --version. You should see the installed Node.js version (e.g., v20.12.1). - Next, type
npm --versionto verify that the Node Package Manager (npm) is installed correctly. You should see a version number (e.g., 10.5.0).
If both commands return version numbers, Node.js is successfully installed. If you encounter an error such as “'node' is not recognized,” restart your terminal or log out and back in to refresh your environment variables.
Installing Node.js on macOS
macOS users have multiple options for installing Node.js, including the official installer, Homebrew, or version managers like nvm. We recommend using nvm (Node Version Manager) for greater flexibility, especially if you plan to work on multiple projects requiring different Node.js versions.
Option 1: Install Node.js via nvm (Recommended)
- Open Terminal (found in Applications > Utilities).
- Install nvm by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash. This downloads and runs the nvm installation script. - After installation, close and reopen your Terminal, or run
source ~/.bashrcorsource ~/.zshrcdepending on your shell (zsh is default on newer macOS versions). - Verify nvm is installed by typing
nvm --version. You should see a version number. - Install the latest LTS version of Node.js by running:
nvm install --lts. - Set the installed version as default:
nvm use --ltsand thennvm alias default lts/*. - Verify the installation:
node --versionandnpm --version. Both should return version numbers.
Option 2: Install via Official Installer
If you prefer a graphical installer:
- Visit https://nodejs.org and download the macOS .pkg file for the LTS version.
- Double-click the downloaded file to open the installer.
- Follow the on-screen prompts: click “Continue,” accept the license, select your disk, and click “Install.”
- Enter your administrator password when prompted.
- Once complete, open Terminal and run
node --versionandnpm --versionto confirm installation.
While the official installer works well, nvm is preferred because it allows you to switch between Node.js versions easily—essential for maintaining compatibility across projects.
Installing Node.js on Linux (Ubuntu/Debian)
Linux users have several options, but the most reliable and widely used method is installing Node.js via the NodeSource repository. This ensures you receive the latest stable version with proper package management.
- Open a terminal window.
- Update your system’s package list:
sudo apt update. - Install curl if it’s not already installed:
sudo apt install curl. - Add the NodeSource repository for the latest LTS version by running:
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -. This script configures the correct APT repository for your distribution. - Install Node.js:
sudo apt install -y nodejs. - Verify the installation:
node --versionandnpm --version.
For advanced users who prefer version management, install nvm on Linux using the same command as macOS: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash. Then reload your shell and use nvm install --lts as described earlier.
Installing Node.js on Linux (Red Hat/CentOS/Fedora)
For RHEL, CentOS, or Fedora systems, use the NodeSource repository as well, but with slight variations.
- Open a terminal.
- Install curl:
sudo yum install curl(for CentOS 7) orsudo dnf install curl(for CentOS 8+/Fedora). - Download and run the NodeSource setup script:
curl -fsSL https://rpm.nodesource.com/setup_lts.x | sudo bash -. - Install Node.js:
sudo yum install -y nodejsorsudo dnf install -y nodejs. - Verify installation with
node --versionandnpm --version.
On Fedora, you may also install Node.js via the default repositories using sudo dnf install nodejs, but this version is often outdated. Always prefer NodeSource for up-to-date releases.
Verifying Your Installation
Regardless of your operating system, always verify your installation with the following commands:
node --version— confirms Node.js is installed and shows the version number.npm --version— confirms npm is installed and shows its version.which node(macOS/Linux) orwhere node(Windows) — shows the installation path.
If any command returns an error, revisit the installation steps. Common issues include:
- Terminal not restarted after installation (especially on Windows).
- Incorrect PATH configuration.
- Conflicting installations from previous attempts.
To resolve conflicts, uninstall any existing Node.js installations before proceeding. On Windows, use “Add or Remove Programs.” On macOS and Linux, remove Node.js directories and nvm folders if necessary.
Best Practices
Always Use the LTS Version
Node.js releases two types of versions: Current and LTS (Long-Term Support). The Current version includes the latest features but may be unstable. The LTS version is thoroughly tested and receives security updates and bug fixes for 30 months. Unless you’re developing a cutting-edge application requiring specific features only available in the Current release, always use the LTS version in production and development environments.
Use a Version Manager (nvm)
One of the most important best practices is using a Node.js version manager like nvm (Node Version Manager). This tool allows you to:
- Install and switch between multiple Node.js versions.
- Set different versions per project.
- Avoid conflicts between applications requiring different Node.js versions.
For example, one project might require Node.js 18 for compatibility with legacy dependencies, while another needs Node.js 20 for modern ES2023 features. With nvm, you can run:
nvm install 18
nvm use 18
nvm install 20
nvm use 20
and switch seamlessly between them. nvm is available on macOS, Linux, and Windows (via nvm-windows).
Keep npm Updated
npm (Node Package Manager) is bundled with Node.js, but it receives frequent updates with performance improvements and security patches. Update npm regularly by running:
npm install -g npm@latest
Always check your npm version with npm --version and update if it’s more than a few months old.
Use a .nvmrc File for Project-Specific Versions
Create a file named .nvmrc in the root of each Node.js project and specify the required Node.js version inside it. For example:
18.17.0
Then, in your project directory, run nvm use (without arguments) to automatically switch to the version specified in .nvmrc. This ensures every developer on your team uses the same Node.js version, eliminating “it works on my machine” issues.
Configure npm Global Packages Securely
By default, npm installs global packages in a system directory that may require elevated permissions. This can lead to permission errors or security risks. To avoid this:
- Create a directory for global packages:
mkdir ~/.npm-global. - Configure npm to use it:
npm config set prefix '~/.npm-global'. - Add the directory to your PATH by adding this line to your shell profile (~/.bashrc, ~/.zshrc, etc.):
export PATH=~/.npm-global/bin:$PATH. - Reload your shell:
source ~/.zshrc(or ~/.bashrc).
This method allows you to install global packages without sudo and keeps your system clean.
Use package-lock.json and Avoid npm install --save-dev Without Context
Always commit your package-lock.json file to version control. This file locks dependency versions and ensures consistent installations across environments. Never ignore it.
When installing packages, use:
npm install package-name— for dependencies required in production.npm install package-name --save-dev— for development tools like linters or test runners.
Use npm ci in CI/CD pipelines instead of npm install for faster, deterministic builds.
Regularly Audit Dependencies
Security vulnerabilities in third-party packages are common. Run npm audit regularly to identify known vulnerabilities in your project’s dependencies. To automatically fix low-risk issues, run:
npm audit fix
For critical vulnerabilities, manually review and update packages or consult the package’s GitHub repository for patches.
Use Environment Variables for Configuration
Never hardcode API keys, database passwords, or other secrets in your source code. Use environment variables instead. Create a .env file in your project root and use the dotenv package to load them:
npm install dotenv
Then, in your Node.js app:
require('dotenv').config();
const dbPassword = process.env.DB_PASSWORD;
Add .env to your .gitignore file to prevent secrets from being committed.
Tools and Resources
Essential Development Tools
Once Node.js is installed, enhance your workflow with these essential tools:
- Visual Studio Code — The most popular code editor for JavaScript and Node.js development. Install the official JavaScript and TypeScript extensions for syntax highlighting, IntelliSense, and debugging.
- Postman or Insomnia — For testing REST APIs during development.
- Git — Version control is mandatory. Install Git and configure your username and email:
git config --global user.name "Your Name"andgit config --global user.email "you@example.com". - nodemon — A utility that automatically restarts your Node.js server when file changes are detected. Install globally:
npm install -g nodemon. Use it by runningnodemon server.jsinstead ofnode server.js. - ESLint — A static code analysis tool to enforce coding standards. Install locally:
npm install eslint --save-dev, then configure withnpx eslint --init. - Prettier — A code formatter that works with ESLint. Install:
npm install prettier --save-dev.
Package Registries and Repositories
npm is the default package registry for Node.js, but there are alternatives:
- npmjs.com — The official registry with over 2 million packages.
- Yarn — An alternative package manager developed by Facebook, known for speed and deterministic installs. Install with:
npm install -g yarn. - pnpm — A fast, disk-space-efficient package manager that uses hard links. Install with:
npm install -g pnpm.
While npm is the standard, pnpm and Yarn offer compelling advantages in large-scale projects.
Documentation and Learning Resources
Always refer to official documentation:
- Node.js Official Documentation — Comprehensive guides, API references, and tutorials.
- npm Documentation — Details on package management, scripts, and configuration.
- Node.js Developer Resources — Tutorials, videos, and curated learning paths.
- freeCodeCamp Node.js Tutorials — Free, project-based learning.
- egghead.io — High-quality video courses on Node.js and related technologies.
Containerization with Docker
For production deployments, consider containerizing your Node.js application using Docker. Create a Dockerfile in your project root:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build and run your container:
docker build -t my-node-app .
docker run -p 3000:3000 my-node-app
This ensures your application runs identically in development, testing, and production environments.
Real Examples
Example 1: Creating a Simple HTTP Server
After installing Node.js, create your first application. In a new directory, run:
mkdir my-first-node-app
cd my-first-node-app
npm init -y
This creates a package.json file. Now create a file named server.js:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
Run the server:
node server.js
Open your browser and navigate to http://localhost:3000. You’ll see “Hello, Node.js!” displayed. This demonstrates Node.js’s ability to handle HTTP requests without external frameworks.
Example 2: Building a REST API with Express
Install Express, a popular web framework:
npm install express
Create api.js:
const express = require('express');
const app = express();
const PORT = 5000;
app.use(express.json());
app.get('/api/users', (req, res) => {
res.json([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]);
});
app.listen(PORT, () => {
console.log(API running on http://localhost:${PORT});
});
Run with node api.js, then visit http://localhost:5000/api/users to see the JSON response. This is a foundational pattern used in thousands of production applications.
Example 3: Using npm Scripts for Automation
Enhance your workflow by adding scripts to package.json:
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "jest",
"lint": "eslint . --ext .js",
"build": "webpack"
}
}
Now you can run:
npm start— to launch the server.npm run dev— to start the server with auto-restart.npm test— to run tests.
This standardizes how team members interact with the application and simplifies deployment scripts.
Example 4: Setting Up a Production-Ready Environment
For a real-world deployment, combine the following:
- Use nvm to lock Node.js version via
.nvmrc. - Use
npm ciin CI/CD to ensure deterministic installs. - Use environment variables via
dotenv. - Run the app with a process manager like PM2:
npm install -g pm2, thenpm2 start server.js --name "my-app". - Set PM2 to start on boot:
pm2 startupandpm2 save.
This setup is used by startups and enterprises worldwide to ensure high availability and minimal downtime.
FAQs
Can I install multiple versions of Node.js on the same machine?
Yes. Using nvm (Node Version Manager), you can install and switch between multiple versions seamlessly. This is essential for maintaining compatibility across different projects.
Do I need to install Python to use Node.js?
Not for basic usage. However, some npm packages that include native C++ extensions (like bcrypt or node-gyp) require Python during installation. On Windows, you may need to install Python and Visual Studio Build Tools. On macOS and Linux, Python is usually pre-installed.
What’s the difference between Node.js and JavaScript?
JavaScript is a programming language. Node.js is a runtime environment that allows JavaScript to execute outside the browser—on servers, desktops, and embedded systems. Node.js provides access to system APIs (file system, network, etc.) that browsers restrict.
Why is npm installing packages so slow?
Slow npm installations are often due to network latency or registry issues. Try switching to a faster registry like https://registry.npmmirror.com (for users in China) or use a proxy. You can also use pnpm or Yarn for faster installs.
Should I use sudo with npm install?
No. Using sudo with npm can cause permission issues and security risks. Instead, configure npm to use a user directory for global packages as described in the best practices section.
How do I uninstall Node.js completely?
On Windows: Use “Add or Remove Programs” to uninstall Node.js, then delete any remaining folders in C:\Program Files\nodejs and C:\Users\YourName\AppData\Roaming\npm.
On macOS: If installed via nvm, run nvm uninstall node. If via .pkg, delete /usr/local/bin/node and /usr/local/lib/node_modules, then reinstall using nvm.
On Linux: Run sudo apt remove nodejs and delete any manually installed files in /usr/local/bin/node or /usr/local/lib/node_modules.
Is Node.js safe to use in production?
Yes. Node.js is used by major companies including Netflix, PayPal, Uber, and LinkedIn. Its event-driven, non-blocking I/O model makes it ideal for high-concurrency applications. However, always follow security best practices: keep dependencies updated, use HTTPS, validate inputs, and avoid running as root.
What should I do if I get an EACCES permission error?
This error occurs when npm tries to write to a directory it doesn’t own. Fix it by reconfiguring npm’s global directory as shown in the “Best Practices” section. Never use sudo to fix this.
Can I use Node.js for desktop applications?
Yes. Frameworks like Electron, Tauri, and Neutralino allow you to build cross-platform desktop apps using Node.js and web technologies (HTML, CSS, JavaScript).
Conclusion
Installing Node.js is a foundational skill for any modern web developer. Whether you’re working on Windows, macOS, or Linux, following the correct procedures ensures a stable, secure, and efficient development environment. By using version managers like nvm, keeping your tools updated, and adopting best practices for dependency and configuration management, you position yourself to build scalable, maintainable applications with confidence.
This guide has walked you through installation on all major platforms, introduced essential tools, demonstrated real-world use cases, and answered common questions. You now have everything you need to begin your Node.js journey. The next step is to start building—whether it’s a simple API, a real-time dashboard, or a full-stack application. Node.js empowers you to write JavaScript everywhere, and with the right setup, you’ll unlock its full potential.
Remember: the key to mastery is consistent practice. Build small projects, contribute to open source, and keep learning. The Node.js ecosystem evolves rapidly, and staying curious will keep you ahead of the curve.