Setting Up Your First Node.js Application Step-by-Step

Search for a command to run...

No comments yet. Be the first to comment.
You open WhatsApp, type a message, and hit send. A single grey tick appears. You're on a train with no signal, but the message went through anyway. Or did it? That single tick is the beginning of a su

You record a Reel, add a song, trim it, and then close the app before posting. When you reopen Instagram, the draft is right there waiting. Nothing was lost. That experience feels simple. The system b

If you have built a React Native app, you have felt the pain of navigation setup. It is easy to get wrong and hard to keep clean as apps grow. This article explains what routing means, why it matters,

Modern apps are not just a pile of screens. They are a living system of features, data flows, offline behavior, and performance constraints. If you have only shipped small apps, the jump to something

Your application has user accounts. Some routes should only be accessible to logged-in users. How do you protect routes and verify that a request actually comes from who they claim to be? JWT (JSON We

Ashish's Blog
57 posts
You want to start with Node.js but don't know where to begin. This guide walks you through installing Node.js, verifying it works, and running your first code. By the end, you'll have a working Node.js application.
Visit nodejs.org and download the LTS (Long Term Support) version. This is the stable, recommended version for most users.
The installer includes both Node.js and npm (Node Package Manager), which you'll need for managing code libraries.
Run the installer and follow the default options. Once complete, open a terminal and verify the installation:
node --version
npm --version
You should see version numbers:
v18.16.0
9.6.7
If you see version numbers, you're good. If you get "command not found," try restarting your terminal or computer.
Open your terminal and create a folder for your project:
mkdir my-first-app
cd my-first-app
Navigate into this folder. Everything you create will live here.
Initialize npm to create a package.json file:
npm init -y
The -y flag skips questions and creates a default package.json. This file tracks your project's metadata and dependencies.
You'll see a new package.json file in your folder.
Create a file called hello.js:
touch hello.js
Open it in your editor and write:
console.log('Hello, Node.js!');
This is the simplest possible Node.js program. It just prints a message.
In your terminal, run:
node hello.js
You should see:
Hello, Node.js!
Congratulations! You've run your first Node.js script.
The REPL (Read-Eval-Print Loop) is an interactive JavaScript environment. It's useful for experimenting.
Type node in your terminal with no filename:
node
You'll see a > prompt:
>
Now you can type JavaScript and execute it immediately:
> console.log('Testing')
Testing
undefined
> 2 + 2
4
> const greeting = 'Hello'
undefined
> greeting
'Hello'
To exit the REPL, press Ctrl+C twice or type .exit and press Enter.
The REPL is great for testing small pieces of code before adding them to your scripts.
Node.js comes with the fs module for reading and writing files. Let's create a data file and read it.
Create a file called data.txt:
echo "This is some data" > data.txt
Now create a script called read-file.js:
import fs from 'fs';
fs.readFile('data.txt', 'utf8', (err, data) => {
if (err) {
console.log('Error reading file:', err);
} else {
console.log('File contents:', data);
}
});
Run it:
node read-file.js
Output:
File contents: This is some data
You're reading a file asynchronously. Node.js reads the file without blocking, and calls the callback when done.
Now let's create a basic HTTP server:
Create a file called server.js:
import http from 'http';
const hostname = 'localhost';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello from Node.js server!');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://\({hostname}:\){port}/`);
});
Run it:
node server.js
You'll see:
Server running at http://localhost:3000/
Open your browser and visit http://localhost:3000/. You'll see your message!
To stop the server, press Ctrl+C in the terminal.
Your project folder now looks like:
my-first-app/
package.json
hello.js
data.txt
read-file.js
server.js
Each file is a standalone script you can run with node filename.js.
Your package.json looks something like:
{
"name": "my-first-app",
"version": "1.0.0",
"type": "module",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Key fields:
npm runNode.js has an enormous ecosystem of packages. Install them with npm.
For example, install Express (a popular web framework):
npm install express
This downloads Express and saves it in a node_modules/ folder. It also updates package.json:
{
"dependencies": {
"express": "^4.18.2"
}
}
Now you can use Express in your code:
import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.send('Hello with Express!');
});
app.listen(3000);
# Install a package
npm install package-name
# Install packages listed in package.json
npm install
# Run a script from package.json
npm run script-name
# List installed packages
npm list
# Update packages
npm update
Use meaningful file names: server.js, utils.js, models.js tell you what each file does.
Keep it organized: As projects grow, organize files into folders like src/, models/, routes/.
Comment your code: Explain why you're doing things, not just what you're doing.
Use the REPL for learning: Test small snippets before adding them to files.
Check documentation: When stuck, Google it or check the official Node.js docs at nodejs.org.
You've learned:
From here, you can explore:
.js filesnode filename.jspackage.json manages your project and dependenciesfs and httpYou now have a solid foundation. Practice creating small scripts, experimenting in the REPL, and building simple servers. The more you practice, the more comfortable Node.js becomes.