Node.js
Node.js runs JavaScript outside the browser: the V8 engine plus built-in modules for files, networks and processes, and no web page. Follow a five-line program from the terminal command, through importing node:fs, reading process.argv and awaiting a file from the disk without blocking, to printing a result and exiting with code 0.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01Which of these does a plain Node.js program NOT have?
Node.js has no web page, so there is no document or window. It adds process and built-in modules instead.
02You run node count.mjs notes.txt. What is process.argv[2]?
process.argv[0] is the path to node, [1] the path to the script, and the words you typed after the script start at index 2.
03What happens while await readFile(file, "utf8") is waiting for the disk?
readFile returns a promise at once and the file read happens outside V8. await pauses only this module until the promise settles.
04text is "eggs\nmilk\nbread\n". What is text.split("\n").length, without trim()?
The final newline leaves an empty piece at the end: ["eggs", "milk", "bread", ""]. trim() removes that newline first, giving 3.
05Where does console.log write in a Node.js program, and when does the program end?
Node's global console writes to standard output. When the script has finished and nothing is pending, Node exits by itself, with exit code 0 on success.
Glossary
- Node.js
- A program that runs JavaScript outside the browser: the V8 engine plus built-in modules and globals such as process.
- V8
- The JavaScript engine from Google, used by Chrome and by Node.js.
- Module
- A file of JavaScript that can import code from other modules and export its own. A .mjs file is a module; node:fs is a module built into Node.js.
- process.argv
- An array of the command's words: the path to node, the path to the script, then any arguments you typed.
- Promise and await
- A promise stands for a result that is not ready yet. It settles when the result arrives (fulfilled) or fails (rejected). await pauses the code until the promise settles.
- Standard output (stdout)
- Where a program's normal output goes; in a terminal, it is the terminal itself.
- Exit code
- A number a program reports when it ends. 0 means success.
- Terminal
- A text window where you type commands, such as node count.mjs notes.txt, and see their output.
Notes and sources
- 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".
- process.argv[0] is the full path of the node program and process.argv[1] the full path of the script, so the exact text depends on your computer. Only index 2 onward are the words you typed after the file name.
- 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.
- Top-level await (await outside a function) works only in modules. Node treats .mjs files as ECMAScript modules; a .js file is one when the nearest package.json says "type": "module", and current releases also treat a .js file with no type field as a module if it uses import or export syntax.
- Both files are real. Save them in one folder and run node count.mjs notes.txt with a current Node.js release; it prints notes.txt: 3 lines.
- Node.js: Introduction to Node.js
- Node.js: Differences between Node.js and the browser
- Node.js API: process.argv
- Node.js API: Exit codes
- Node.js API: fsPromises.readFile()
- Node.js API: Built-in modules
- Node.js API: ECMAScript modules
- Node.js: Don't block the event loop (or the worker pool)
- MDN: await
- MDN: String.prototype.split()
- ECMAScript Language Specification (ECMA-262)
Quiz Unlocks after the last step.