Node.jsLesson 1 of 13

What is Node.js?

Node.js is a program that runs JavaScript on your computer instead of inside a browser. Before Node.js existed, JavaScript only worked in web pages. It could make buttons clickable and menus drop down, but it could not touch files or answer network requests. Ryan Dahl changed that in 2009. He took the V8 engine from Google Chrome and gave it tools to talk to the operating system. The result lets you write the language you already know and use it to build servers, command line tools, and more.

Node.js is a program that runs JavaScript on your computer instead of inside a browser. Before Node.js existed, JavaScript only worked in web pages. It could make buttons clickable and menus drop down, but it could not touch files or answer network requests. Ryan Dahl changed that in 2009. He took the V8 engine from Google Chrome and gave it tools to talk to the operating system. The result lets you write the language you already know and use it to build servers, command line tools, and more.

Node.js is not a framework or a library. It is a runtime, the environment that executes your code. When you write JavaScript for a browser, the browser is the runtime. When you write it for Node.js, Node.js is the runtime, and it adds extras the browser does not offer: reading and writing files, making network requests, and serving web pages. Node.js ships with a small core and a large collection of built-in modules, and you can add thousands more from npm.

The event loop is the part of Node.js that keeps everything moving. In plain words, Node.js works on one main thread, so it cannot do two things at once. Instead, when it starts a slow job like reading a file, it hands the work to the operating system and moves on to the next task. When the file is ready, the operating system calls back, and Node.js runs the code that was waiting. That is why one small Node.js server can answer many visitors at the same time without slowing down.

Why does this matter to you? Because the web runs on servers, and servers need someone to answer requests. Companies like Netflix, LinkedIn, and PayPal run Node.js in production. You already know JavaScript, so you can build real backend software without learning a second language. The next few lessons take you from install to a working web app.

Client-side vs server-side

Code that runs in a browser is client-side code. It runs on the visitor's computer, inside a sandbox that blocks access to files and to the network beyond the page. Code that runs on a server answers requests from many visitors at once, and it can touch files, databases, and other services. Node.js is built for that second job. This is why the same JavaScript behaves differently in a browser and in Node.js.

JS
// Browsers block file access on purpose
// Node.js can read your disk
const fs = require("fs");
const data = fs.readFileSync("notes.txt", "utf8");
console.log(data);

See if you have it

Before you go further, check whether Node.js is already on your computer. Open a terminal and run node --version. If it prints something like v20.11.0, you are ready. If the terminal says the command was not found, the next lesson walks you through the install.

JS
node --version

Why not just use the browser?

Beginners often paste Node.js code into an HTML file and wonder why nothing happens. The browser cannot run require, read files, or listen on ports. Those tools only exist in the Node.js runtime. If you see an error like require is not defined, the code is running in the browser instead of Node.js. Server code needs a terminal.

JS
// In the browser this line throws: require is not defined
// In Node.js the same line works
const http = require("http");

Syntax

JS
const http = require("http"); // built-in module, no install needed

const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("Hello from Node.js"); // the reply the browser receives
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Node Is Not Browser — No window/document

Node has no window, document, or alert — it has global, process, and fs. Code using window will crash.

JS
globalThis // not window

Good to know

  • Node.js runs JavaScript on your computer, not in a browser.
  • A runtime is the environment that executes your code.
  • The event loop handles slow jobs without freezing the program.
  • You cannot run Node.js code inside an HTML page.
Try it Yourself Edit the code, press Run, then check what changed in the Result.
Ready
Code
Result

Practice makes it stick

Open the Playground and build your own page with the tags you just learned.

→ Open Playground