Node.js BasicsBeginner7 min01 / 8

What is Node.js?

Discover how Node.js lets you run JavaScript outside the browser — on servers, in terminals, and everywhere in between.

You already know that JavaScript makes web pages interactive. But here is a question worth sitting with: why should JavaScript only run inside a browser? The logic you write — loops, functions, data manipulation — has nothing inherently "browsery" about it. Node.js is the technology that answered that question back in 2009, and it changed what JavaScript developers could build forever. With Node.js, the same language you use to animate a button can also power a web server, read files from your hard drive, talk to a database, or build a command-line tool. One language, everywhere.

#JavaScript Needs an Engine

JavaScript is just text — a list of instructions written for humans to read. For a computer to actually run those instructions, something needs to translate them into machine code. In browsers, that translator is called a JavaScript engine. Google Chrome uses one called V8, and it is blazingly fast.

Node.js takes that exact same V8 engine and packages it so you can run it straight from your terminal — no browser required. When you type node myfile.js, V8 springs to life, reads your file, and executes it.

Think of it like

V8 is like a universal translator

Imagine you wrote a letter in French. A French person (the browser) can read it, but so can a French translator living in another country (Node.js). The letter — your JavaScript — is the same. Only the reader changed. Node.js is that translator living outside the browser, able to deliver your message to entirely different audiences: a server, a database, a file system.

## What You Can Build with Node.js

Node.js shines wherever a program needs to handle lots of simultaneous connections or I/O operations (I/O means reading and writing — files, networks, databases). Common use cases:

  • Web servers and REST APIs — the backend your React app talks to
  • Command-line tools (CLIs) — automation scripts you run in the terminal
  • Real-time applications — chat apps, live dashboards, collaborative editors
  • Build tools — bundlers like Webpack and Vite are Node.js programs
  • Serverless functions — small units of server logic deployed to the cloud

Netflix, LinkedIn, and Uber run significant parts of their infrastructure on Node.js.

#Running a File with the node Command

bash
# Check that Node.js is installed
node --version

# Run a JavaScript file
node hello.js
Identical JavaScript syntax — the same console.log you already know — just running outside the browser.
// hello.js
console.log("Hello from Node.js!");

const name = "world";
console.log(`Greetings, ${name}.`);

#No DOM — A Different Set of APIs

In the browser, JavaScript comes pre-loaded with the DOM — APIs like document.querySelector and addEventListener. Node.js has no browser, so it has no DOM. Instead, it ships with built-in modules suited for server-side work: fs (files), http (web servers), path (file paths), and os (operating system info). You load them with require. Here is what reading a file looks like:

The built-in 'fs' module lets Node read, write, and delete files — something the browser deliberately prevents for security.
// Reading a file from disk — impossible in a plain browser script
const fs = require("fs");

const contents = fs.readFileSync("hello.js", "utf8");
console.log(contents);
Common mistake

Browser APIs do not exist in Node.js

document, window, localStorage, and alert are all browser-only. If you paste browser code into a Node file and run it, you will get ReferenceError: document is not defined. Always ask: "Am I writing this for a browser or for Node?" The answer changes which APIs are available to you.

Run with 'node server.js', then open http://localhost:3000 in your browser. You will see your message.
// server.js — the smallest possible web server
const http = require("http");

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

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

You try to run `document.querySelector('#app')` inside a Node.js script. What happens?

Key takeaways

  • Node.js runs JavaScript outside the browser using Google's V8 engine.
  • There is no DOM in Node.js — instead you get file system, network, and OS APIs.
  • You run a JavaScript file with Node by typing `node filename.js` in your terminal.
  • Node.js is used to build web servers, REST APIs, CLIs, real-time apps, and build tools.
  • All the JavaScript syntax you already know works exactly the same in Node.js.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

This is hello.js from the lesson, run with `node hello.js`. What does it print?

predict-output
// hello.js
console.log("Hello from Node.js!");

const name = "world";
console.log(`Greetings, ${name}.`);
Fix the bug#2

This code is pasted from a browser script into a file and run with `node app.js`. What's wrong?

fix-bug
// app.js
const app = document.querySelector('#app');
app.textContent = 'Loaded!';
Fill in the blank#3

Complete this Node.js line so it loads the built-in file-system module (as shown in the lesson).

const fs = ("fs");
const contents = fs.readFileSync("hello.js", "utf8");
Reorder the lines#4

Order these lines to build the smallest possible Node.js web server from the lesson.

1
const http = require("http");
2
server.listen(3000);
3
const server = http.createServer((req, res) => {
4
});
5
  res.end("Hello from my Node server!\n");
Your turn
Practice exercise

Create a Node.js script that prints a friendly server startup message. It should: (1) read a port value from process.env.PORT, defaulting to 3000 if that variable is not set, (2) print a message saying the server would start on that port, and (3) also print the current Node.js version using process.version. No actual server is needed — just the console output.

Try it yourself — a starting point to build on:

starter.js
// startup.js
// TODO: Read the port from process.env.PORT, default to 3000 if not set
const port = /* your code here */;

// TODO: Print a startup message that includes the port number
console.log(/* your message */);

// TODO: Print the Node.js version using process.version
console.log(/* node version message */);