What Node.js is
Node.js runs JavaScript outside the browser. It is the V8 engine, the same one Chrome uses, plus built-in modules for files, networks and processes. There is no web page, so there is no document or window.
Starting a program
In a terminal you type node count.mjs notes.txt. Node reads the file count.mjs and hands it to V8. The .mjs ending marks the file as a module: a file that can import code from other modules.
Line 1 imports readFile from node:fs/promises. The node: prefix names a module built into Node, and fs means file system. Browsers have no such module. readFile is now a function the script can call.
process is a global object that Node adds. process.argv is an array of the command's words: the path to node, the path to count.mjs, then notes.txt. Index 2 is "notes.txt", so file holds it.
Reading a file
readFile asks the operating system to read notes.txt and returns a promise at once. await pauses this module until the promise settles. While the disk works, V8 is free: nothing is blocked.
The bytes come back. Node decodes them as UTF-8 (the common code from bytes to letters), the promise is fulfilled, and await resumes line 3. text now holds "eggs\nmilk\nbread\n": three lines, each ending in a newline.
Results and exit
Line 4 is plain JavaScript, as in a browser. trim() drops the last newline, split("\n") cuts the text into ["eggs", "milk", "bread"], and length is 3. Without trim, split would give 4 pieces.
JavaScript first fills in the template string, giving notes.txt: 3 lines. console.log then writes it to standard output, which is the terminal. Node has no developer tools panel: output appears where you typed the command.
No lines are left and no work is waiting. Node exits by itself with exit code 0, which means success, and the terminal shows its prompt again, ready for the next command.
What happens
// step 1/9 · node-is
Node.js = V8 (runs JavaScript)
+ built-in modules (node:fs, node:http, ...)
+ globals such as process
missing document, window: there is no page
language the same ECMAScript as in the browser
ECMAScript (ECMA-262) defines the language; a host adds the objects around it. A browser adds document and window; Node.js adds process, require (in CommonJS files) and built-in modules such as node:fs. In Node, typeof document is "undefined". Node.js hands file reads to a small pool of helper threads (its worker pool, from the libuv library). V8 keeps running other JavaScript meanwhile; this program simply has nothing else to do.