Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

What Node.js is

Node.js is an environment that runs JavaScript outside the browser: on your computer or on a server. The language is the same one you learned in the tutorial, but instead of a page and buttons the program gets access to files, the network and the operating system. Node.js is used to write web servers, command-line utilities and build tools.

Version

The course targets Node.js 22 and newer. Even-numbered versions (22, 24) get LTS status — long-term support: they receive fixes for several years, so they are the ones installed on servers. All examples use ES modules (import and export) and built-in modules with the node: prefix.

Installing

Download the installer from nodejs.org — pick the version marked LTS. Then open a terminal and check the result:

node --version
npm --version

The first command prints the Node.js version, for example v22.12.0, the second the version of npm, the package manager that comes with Node.js.

How Node.js differs from the browser

In the browserIn Node.js
document and window existThey do not: there is no page
No access to filesFiles can be read and written
Code comes from a websiteCode lives on your computer
Network limited by browser rulesAny request can be sent and received

The language itself — variables, functions, classes, promises — works the same way.

What we will build

One example runs through the whole course — a small blog. First you will learn to keep posts in a JSON file, then you will write an HTTP server that returns them as a list and one by one, and accepts new posts and comments. Everything uses built-in modules, no frameworks. Once you see how a server works by hand, it becomes clear what Express and other frameworks do for you.

Course outline

ChapterAbout
Getting startedRunning programs, package.json, modules, npm
Program and environmentArguments, environment variables, paths
FilesReading, writing, folders, streams
AsynchronyThe event loop, events, errors
HTTP without frameworksA server, routes, a JSON API, fetch
Tools and practiceTests, debugging, a database, deployment

Common mistake

Code written for the browser does not work in Node.js: there are no page objects here.

// document.querySelector('h1');
// ReferenceError: document is not defined
 
console.log(typeof document); // undefined

If you see document is not defined, a script meant for a web page was run with node. Such code is attached to an HTML page, not executed in the terminal.

Thanks for reading!

Donate to the site — we work hard for you.