Setting up your Node.js development environment is the first step toward becoming a full-stack JavaScript developer. In this article, you’ll learn how to install Node.js, configure your project, and run your first application.
Table of Contents
- What is Node.js?
- Why Use Node.js?
- Installing Node.js
- Verifying Installation
- Initializing a Project with npm
- Running a Simple Node.js App
- Installing Dependencies
- Conclusion
1. What is Node.js?
Node.js is a JavaScript runtime built on Chrome’s V8 engine that allows you to run JavaScript outside the browser. It uses a non-blocking, event-driven model, making it lightweight and efficient for I/O-heavy applications like APIs and real-time services.
2. Why Use Node.js?
- Fast & Scalable: Perfect for high-performance applications.
- JavaScript Everywhere: Use JS for both frontend and backend.
- Large Ecosystem: Thanks to npm (Node Package Manager).
- Cross-Platform: Runs on Windows, macOS, and Linux.
3. Installing Node.js
Windows/macOS
- Go to: https://nodejs.org
- Download the LTS version.
- Run the installer and follow the instructions.
Linux (Debian/Ubuntu)
sudo apt update
sudo apt install nodejs npm
4. Verifying Installation
After installation, open your terminal and run:
node -v
npm -v
If both commands return version numbers, you’re good to go!
5. Initializing a Project with npm
Step 1: Create a project folder
mkdir my-node-app
cd my-node-app
Step 2: Initialize with npm
npm init -y
This creates a package.json
file with default values.
6. Running a Simple Node.js App
Create a file named app.js
and paste the following code:
const http = require('http');
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello, Node.js!');
});
server.listen(3000, 'localhost', () => {
console.log('Server running at http://localhost:3000/');
});
Run it using:
node app.js
Now, open your browser and go to http://localhost:3000/
.
7. Installing Dependencies
Install any package using:
npm install <package-name>
Example: installing express
npm install express
Add dev dependencies (like testing tools):
npm install --save-dev <package-name>
To remove a package:
npm uninstall <package-name>
Conclusion
You’ve now set up your first Node.js development environment! From installing Node and npm to creating your first app, you’re ready to start building server-side applications using JavaScript.
Next, we’ll explore how to build routes, use Express, and connect to databases.