Skip to main content

Command Palette

Search for a command to run...

Getting Started with Node.js

Updated
•4 min read•View as Markdown

Why Do We Need Node.js?

Imagine you already know JavaScript from the browser.

You can:

  • change webpage content

  • handle button clicks

  • create interactive UI

But what if you want JavaScript to:

  • create servers

  • read files

  • build APIs

  • work outside the browser

That’s where Node.js comes in.

Node.js allows JavaScript to run on your computer directly, not just inside a browser.


What is Node.js?

Node.js is a JavaScript runtime.

Simple meaning:

It lets your computer understand and run JavaScript files.

Without Node.js:

JavaScript → Browser only

With Node.js:

JavaScript → Browser + Computer
Image

How Node.js Works

Image

Installing Node.js

To install Node.js:

  1. Go to the official website of Node.js

  2. Download the LTS version

  3. Install normally like other software

LTS means:

Long Term Support (stable version)


Checking if Node.js is Installed

Open your terminal and run:

node -v

If installed correctly, you’ll see something like:

v22.5.0

Check npm Too

npm comes with Node.js.

Run:

npm -v

You’ll see a version number.

npm helps install packages later.


What is the Node REPL?

Before writing files, Node.js gives us something called REPL.

REPL stands for:

  • Read

  • Evaluate

  • Print

  • Loop

Sounds complex, but it’s simple.

Think of it like a JavaScript playground inside your terminal.


Starting the REPL

Run:

node

You’ll see:

>

Now you can directly write JavaScript.

Example:

2 + 3

Output:

5

Why REPL is Useful

REPL is great for:

  • quick testing

  • practicing JavaScript

  • checking small logic

Example:

let name = "Aman";
console.log(name);

Instant output.

No file needed.


Exiting the REPL

To exit:

.exit

Or press:

Ctrl + C (twice)

Creating Your First JavaScript File

Create a file:

app.js

Inside it:

console.log("Hello Node.js");

Running the File

Open terminal in the same folder and run:

node app.js

Output:

Hello Node.js

Your First Node.js Server

Now let’s create a tiny web server.

Replace your code with this:

const http = require('http');

const server = http.createServer((req, res) => {
  res.end('Hello World');
});

server.listen(3000, () => {
  console.log('Server running on port 3000');
});

Running the Server

Run:

node app.js

Terminal output:

Server running on port 3000

Now open browser and visit:

http://localhost:3000

You’ll see:

Hello World

Breaking Down the Server Code

Importing HTTP Module

const http = require('http');

Node.js already provides an HTTP tool.

We import it.


Creating the Server

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

});

This creates a server.

  • req → incoming request

  • res → response we send back


Sending Response

res.end('Hello World');

This sends text to the browser.


Starting the Server

server.listen(3000);

This starts the server on port 3000.

Think of a port like a door your app listens on.


Before vs After Node.js

Without Node.js

JavaScript only inside browser

With Node.js

JavaScript can create servers, APIs, tools, scripts

Much more powerful.


Practice Assignment

1. Check Installation

Run:

node -v
npm -v

2. Use REPL

Start REPL:

node

Try:

  • addition

  • variables

  • console.log


3. Create First File

Create:

test.js

Write:

console.log("Learning Node.js");

Run it.


4. Modify the Server

Change:

Hello World

to your own custom message.


5. Change Port Number

Try:

  • 4000

  • 5000

Observe what changes in browser URL.