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.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
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#
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#
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();
});
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#
- console.log prints to the terminal, not to a browser console.
- process.version is a Node-only global with the runtime version.
- http.createServer receives a function that runs for every request.
- listen(3000) starts accepting connections on port 3000.
- 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#
document, window and alert do not exist in Node. Use Node modules like fs and http.
A 2-second calculation blocks every other user for 2 seconds. Offload it to a worker thread or a job queue.
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?
Show solution
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#
Which engine does Node.js use to run JavaScript?
Want more? Practise all Node.js MCQs · Node.js interview questions
My notes#
Saved in this browser.