Skip to main content

Command Palette

Search for a command to run...

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

Updated
•2 min read•View as Markdown

Installing Node.js

Node.js must first be installed before running JavaScript outside the browser.

Installing Node.js also installs:

  • Node runtime

  • npm package manager

After installation, JavaScript files can be executed directly from the terminal.


Checking Installation Using Terminal

After installation, verify Node.js using:

node -v

This displays the installed Node.js version.

Check npm version:

npm -v

Understanding Node REPL

REPL stands for:

  • Read

  • Evaluate

  • Print

  • Loop

It is an interactive Node.js environment where JavaScript code can be tested directly.

Start REPL using:

node

Example:

> 2 + 2
4

Exit REPL using:

.exit

Creating First JS File

Create a JavaScript file:

index.js

Add code:

console.log("hello world");

Running Script Using node Command

Run JavaScript files using:

node index.js

Output:

hello world

Node.js executes the file using the V8 engine.


Writing Hello World Server

Node.js can also create servers.

Example:

const http = require("http");

const server = http.createServer((req, res) => {

  res.end("Hello World");

});

server.listen(3000, () => {
  console.log("server running");
});

Now visiting:

http://localhost:3000

shows:

Hello World

Initializing a Project

Most Node.js projects start using:

npm init -y

or:

pnpm init

This creates:

package.json

which stores project configuration and dependencies.


Initializing TypeScript

For TypeScript projects:

tsc --init

This creates:

tsconfig.json

which contains TypeScript configuration.


Installing Express

Express is one of the most popular Node.js frameworks.

Install using:

npm install express

or:

pnpm add express

Basic Express Server Snippet

const express = require("express");

const app = express();

app.get("/", (req, res) => {

  res.send("Hello from Express");

});

app.listen(3000, () => {
  console.log("server running");
});

This creates a simple Express server running on port 3000.


Common Daily Starter Setup

A common backend setup flow:

mkdir project-name

cd project-name

npm init -y

npm install express

tsc --init