Setting Up Your First Node.js Application Step-by-Step
1. Installing Node.js
Before you start building any Node.js application, the first step is to install Node.js on your system. The good news is—it’s very simple and beginner-friendly.
Step 1: Download Node.js
Go to the official website of Node.js.
You’ll see two versions:
LTS (Long Term Support) → Recommended for beginners (stable)
Current → Latest features but may be less stable
👉 Always download the LTS version.
Step 2: Install Node.js
Once downloaded:
Open the installer
Click Next → Next → Install
Keep everything as default (no need to change anything)
Node.js installation also installs npm (Node Package Manager) automatically, which helps you install libraries later.
Step 3: Verify Installation
After installation, open your terminal (Command Prompt / PowerShell / Terminal) and run:
node -v
If Node.js is installed correctly, you’ll see a version like:
v18.x.x
Now check npm:
npm -v
This will show the npm version.
Example
If both commands return versions, you’re ready to go 🎉
node -v → v18.17.0
npm -v → 9.6.7
Why This Matters
Node.js allows you to run JavaScript outside the browser, which means you can build servers, APIs, and full backend applications.
2. Checking installation using terminal
After installing Node.js, the next step is to make sure everything is working correctly. This is done using the terminal (Command Prompt, PowerShell, or Mac/Linux Terminal).
Step 1: Open Terminal
Windows: Open Command Prompt or PowerShell
Mac/Linux: Open Terminal
Step 2: Check Node.js Version
Type the following command:
node -v
If Node.js is installed properly, you will see a version number like:
v18.17.0
Step 3: Check npm Version
Now check if npm (Node Package Manager) is also installed:
npm -v
You should see something like:
9.6.7
Example
A successful setup will look like this:
node -v → v18.x.x
npm -v → 9.x.x
What If It Doesn’t Work?
If you see an error like “node is not recognized”, it usually means Node.js is not installed properly or not added to your system path.
In that case, reinstall Node.js or restart your system once.
Why This Step is Important
This quick check confirms that your system can actually run Node.js and install packages using npm. Without this, you won’t be able to build or run your application.
3. Understanding Node REPL
Once you have Node.js installed, you can start using something called the REPL.
REPL stands for:
R → Read
E → Evaluate
P → Print
L → Loop
In simple terms, it’s an interactive environment where you can write JavaScript code and see the result instantly.
Step 1: Start the REPL
Open your terminal and type:
node
You’ll see something like:
>
This > symbol means you are now inside the Node REPL.
Step 2: Try Simple JavaScript
You can directly write JavaScript code here:
2 + 3
Output:
5
Or try variables:
let name = "Alok";
name
Output:
'Alok'
Step 3: Use Built-in Features
You can also use JavaScript functions:
Math.sqrt(16)
Output:
4
Useful REPL Commands
.help→ Shows all available commands.clear→ Clears the screen.exit→ Exit the REPL
Why REPL is Useful
Quick testing of code snippets
Learning JavaScript interactively
Debugging small logic without creating files
Example Use Case
Suppose you want to test a small function:
function greet(name) {
return "Hello " + name;
}
greet("Alok")
Output:
'Hello Alok'
No file needed—everything runs instantly.
4. Creating first JS file
Now that you’ve explored the REPL in Node.js, it’s time to create your first actual JavaScript file. This is how real applications are built.
Step 1: Create a File
Open any code editor like VS Code or even Notepad.
Create a new file and name it:
app.js
👉 .js extension is important because it tells Node.js that this is a JavaScript file.
Step 2: Write Your First Code
Add a simple line of code inside app.js:
console.log("Hello, Node.js!");
This will print a message to the terminal.
Step 3: Run the File
Open your terminal in the same folder where your file is saved and run:
node app.js
Output
You’ll see:
Hello, Node.js!
🎉 Congrats! You just ran your first Node.js program.
Example with More Code
Let’s try a slightly bigger example:
const name = "Alok";
function greet(user) {
return "Welcome " + user;
}
console.log(greet(name));
Output:
Welcome Alok
Why This Step is Important
This is how real Node.js apps start
You can now write multiple lines of code
Helps you build scripts, servers, and full applications
5. Running script using node command
After creating your JavaScript file, the next step is to actually run it using Node.js. This is where your code comes to life.
Step 1: Open Terminal in Your Project Folder
Make sure your terminal is opened in the same folder where your file (like app.js) exists.
Example:
cd your-folder-name
Step 2: Run the Script
Use the node command followed by your file name:
node app.js
Output
If your file contains:
console.log("Hello, Node!");
You’ll see:
Hello, Node!
Example with Logic
Let’s try a slightly more useful example:
const a = 10;
const b = 20;
console.log("Sum is:", a + b);
Run it:
node app.js
Output:
Sum is: 30
Common Mistakes
❌ Wrong file name →
node app(missing.js)❌ Wrong folder → terminal not in correct directory
❌ File not saved before running
Pro Tip 💡
You can run any .js file like this:
node filename.js
No need for any extra setup—Node handles everything.
Why This Matters
This command is the core of Node.js development. Whether you're building a small script or a full backend server, you’ll always use node filename.js to run your code.
6. Writing Hello World server
Now comes the exciting part—creating your very first server using Node.js. This is how backend development actually begins 🚀
Step 1: Create a New File
Create a file named:
server.js
Step 2: Write Server Code
Add the following code:
const http = require("http");
const server = http.createServer((req, res) => {
res.end("Hello World");
});
server.listen(3000, () => {
console.log("Server is running on port 3000");
});
Step 3: Run the Server
Open terminal and run:
node server.js
You’ll see:
Server is running on port 3000
Step 4: Open in Browser
Go to your browser and type:
http://localhost:3000
🎉 You’ll see:
Hello World
How This Works (Simple Explanation)
http.createServer()→ Creates a serverreq→ Request from client (browser)res→ Response sent backres.end()→ Sends response and ends requestserver.listen(3000)→ Runs server on port 3000
Example: Custom Message
You can change the message easily:
res.end("Welcome to my first Node.js server!");
Why This is Important
This is the foundation of all backend apps
APIs, websites, and real-world apps all start like this
You now understand how client (browser) and server communicate
