Node.js Tutorial menu

Node.js Introduction

Node.js is a runtime that lets you run JavaScript on computers and servers, not only in web browsers.

What is Node.js?

  • Node.js is free and open source
  • It runs on Windows, macOS and Linux
  • It uses V8, the same fast JavaScript engine that is inside Google Chrome
  • It was created by Ryan Dahl in 2009
  • It comes with npm, the largest collection of free code packages in the world

Node.js is not a programming language and not a framework. The language is JavaScript. Node.js is the program that runs it.

Browser JavaScript vs Node.js

In the browserIn Node.js
Runs insideChrome, Edge, FirefoxYour computer or a server
Has document and windowYesNo
Can read and write filesNoYes
Can start a web serverNoYes
Global objectwindowglobalThis (also global)
Main jobUser interfaceBack end, tools, scripts

The language rules are the same: variables, functions, arrays, objects, promises and classes all work as you know them.

What Can You Build?

  • Web servers and APIs that send data to websites and mobile apps
  • Real-time apps such as chat, live scores and online games
  • Command-line tools, like the ones you use to create React or Angular projects
  • Scripts to rename files, send emails or process CSV data
  • Desktop apps with tools like Electron (VS Code itself is built this way)

Why is Node.js Fast?

Imagine a waiter at a busy restaurant in Burns Road. A slow waiter takes one order, waits in the kitchen until the food is ready, serves it, and only then takes the next order.

A smart waiter takes an order, gives it to the kitchen, and immediately takes the next order. When food is ready, the kitchen calls, and the waiter serves it.

Node.js works like the smart waiter. It is non-blocking: when it needs to wait for a file, a database or the network, it starts the job and moves on. When the job finishes, Node.js runs your code for the result.

Example

js
const fs = require("fs");

console.log("1. Start reading the file");

fs.readFile("notes.txt", "utf8", (err, text) => {
  console.log("3. File is ready");
});

console.log("2. Doing other work while waiting");
Output
1. Start reading the file
2. Doing other work while waiting
3. File is ready

Line 2 prints before line 3, because Node.js does not stop and wait for the file.

The Event Loop

Your JavaScript in Node.js runs on one main thread. The event loop keeps checking for finished jobs (a file read, a network reply, a timer) and runs their callbacks one at a time. This is how a single thread can handle thousands of users at once.

Because there is one main thread, a very heavy calculation (like a huge loop) blocks everyone. Node.js is best for apps that do a lot of input and output, not heavy number crunching.

Versions

Node.js releases a new major version every six months. Even numbers (20, 22, 24) become LTS (Long Term Support) versions, which are supported for about three years. Always use an LTS version for real projects.

Exercise

Exercise

Which JavaScript engine does Node.js use?

Learn Node.js with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp