Node.js · Getting Started

Introduction to Node.js

Node.js lets you run JavaScript outside the browser — on servers, laptops and build machines. The same language you use for web pages can read files, talk to databases and serve APIs.

Beginner 5 min Lesson 1 of 37Examples target Node.js 22 LTS.
0/37 in Node.js
On this page · 5 min · Beginner

Learning objectives#

  • Explain what Node.js is
  • Know what Node.js is good and not so good at
  • Run a script with node
  • Start a tiny web server

Explanation#

Node.js is a JavaScript runtime built on Chrome’s V8 engine. Instead of browser APIs like document and window, it gives you server APIs: files (fs), networking (http), processes and more.

Node uses non-blocking, event-driven I/O. While it waits for a database or a file, it keeps serving other requests. That makes it efficient for APIs, real-time apps (chat, live dashboards) and tooling.

It is less suited to heavy CPU work such as video encoding or large calculations in the main thread — those block every other request (use worker threads or another service).

npm, Node’s package manager, gives you millions of libraries. Tools you already use — Vite, ESLint, the Angular CLI — are Node programs.

Install the LTS version, then run a file with node app.js. Node 22 can run .js files written as ES modules when package.json has "type": "module".

Syntax#

Shell Illustrative
node --version        # v22.x
node hello.js         # run a script
node                  # interactive REPL (Ctrl+C twice to exit)

Code example: A script and a web server in one file#

JavaScripthello.js Illustrative
import http from 'node:http';


console.log(`Hello from Node.js ${process.version.split('.')[0]}!`);
console.log('2 + 3 =', 2 + 3);


const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from my first Node.js server');
});


server.listen(3000, async () => {
  console.log('Server running on http://localhost:3000');


  // Try it from the same script (normally you would open the URL in a browser)
  const response = await fetch('http://localhost:3000');
  console.log('Browser would show:', await response.text());
  server.close();
});
Expected output
Hello from Node.js v22!
2 + 3 = 5
Server running on http://localhost:3000
Browser would show: Hello from my first Node.js server

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. console.log prints to the terminal, not to a browser console.
  2. process.version is a Node-only global with the runtime version.
  3. http.createServer receives a function that runs for every request.
  4. listen(3000) starts accepting connections on port 3000.
  5. fetch (built into Node 18+) calls the server; server.close() lets the script exit.

Real-world use case#

A startup builds its REST API, its admin scripts and its React front-end tooling all in JavaScript/TypeScript with Node.js, so one team can work across the whole stack.

Common mistakes#

Using browser APIs

document, window and alert do not exist in Node. Use Node modules like fs and http.

CPU-heavy work in request handlers

A 2-second calculation blocks every other user for 2 seconds. Offload it to a worker thread or a job queue.

Installing an odd-numbered version for production

Odd versions (21, 23) are short-lived. Use the current LTS (even) version.

Best practices#

  • Use the LTS version
  • Keep request handlers non-blocking
  • Use the node: prefix for built-in modules
  • Learn async JavaScript well — Node relies on it

Remember this#

Node.js = JavaScript + server APIs (files, network, processes).

Great for I/O; careful with CPU-heavy work.

node file.js runs a file.

Practice exercise#

Your task: Write a script that prints today’s weekday name and the number of seconds the script has been running (process.uptime()).

Need a hint?
new Date().toLocaleDateString("en-IN", { weekday: "long" }).
Show solution
JavaScript Illustrative
const weekday = new Date().toLocaleDateString('en-IN', { weekday: 'long' });
console.log(`Today is ${weekday}`);
console.log(`Script running for ${process.uptime().toFixed(2)} s`);

Mini quiz#

Introduction to Node.js quizMultiple choice Question 1 of 3

Which engine does Node.js use to run JavaScript?

Score so far: 0/0

Want more? Practise all Node.js MCQs · Node.js interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.