New to Node.js? This guide walks you through setting up your first Node.js project from scratch—whether you’re building APIs, CLI tools, or learning backend development.
1. What is Node.js?
Node.js is a runtime environment that allows you to run JavaScript on the server side. It’s fast, event-driven, and perfect for building scalable web apps and services.
2. Install Node.js
Download and install Node.js from the official website:
Once installed, verify it in your terminal:
node -v
npm -v
3. Create Your Project Folder
Open your terminal and create a new directory for your project:
mkdir my-first-node
cd my-first-node
4. Initialize a New Node.js Project
Use npm (Node Package Manager) to create a package.json
file:
npm init -y
This will generate a basic configuration file to manage your project dependencies.
5. Create Your First Script
Make a file named app.js
in your project folder:
// app.js
console.log("Hello, Node.js!");
6. Run Your Node.js Script
In your terminal, run the script using:
node app.js
You should see the output: Hello, Node.js!
7. Installing External Packages
To extend your project, you can install packages like express
:
npm install express
Then require and use them in your files as needed.
8. Create a Simple Server (Bonus)
// app.js
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Your first Node.js server is running!");
});
server.listen(3000, () => {
console.log("Server is listening on port 3000");
});
Now run it again using node app.js
and visit http://localhost:3000
in your browser.
Final Thoughts
Congrats! You’ve set up and run your first Node.js project. From here, you can explore frameworks like Express, build REST APIs, or dive into full-stack development.
Enjoyed this guide? Follow me on The Learn with Rana for more tutorials. Let’s also connect on LinkedIn to share developer tips and tricks!
Comments
Post a Comment