Core Node.js modules: fs, path, http, events
The built-in modules the exam expects you to reach for: fs in its sync, callback and promise forms, path.join versus path.resolve and __dirname, http.createServer, EventEmitter ordering and the 'error' event, child_process, crypto hashing and util.promisify, and when a framework such as Express is worth a dependency.
Know the core Node.js modules and given requirements, infer which Node.js library/framework is a good solution.
~6 min read · 7 practice questions ·
What the exam expects
- Choose the fs form (synchronous, callback,
fs/promises) that fits a requirement, and spot the bug when the callback API is awaited. - Predict
path.join()andpath.resolve()output, and build file paths that do not depend on the working directory. - Build a dependency-free HTTP server with
http.createServer()and know when a framework such as Express earns its place. - Predict the order of
EventEmitterlistener calls, includingonce()and the'error'event. - Pick the module for a job:
child_processto run a command,cryptoto hash,util.promisifyto adapt a callback API.
Key terms: core module (ships with Node.js, loaded with the node: prefix, no npm install); error-first callback (a callback whose first argument is reserved for an error); __dirname (directory of the current CommonJS module); EventEmitter (object that calls registered listeners when a named event is emitted); framework (a third-party package such as Express that adds routing and middleware on top of http).
Key facts
- Detail
- Every operation has synchronous, callback and promise-based forms
- Detail
- First callback argument is reserved for an exception
path.resolve()- Detail
- Right to left until absolute; falls back to the working directory
__dirname- Detail
- Directory name of the current module
import.meta.dirname- Detail
- ESM equivalent for
file:modules; no longer experimental since v24.0.0 and v22.16.0
http.createServer()- Detail
- Returns an
http.Server; the listener is added to'request'
- Detail
- Synchronous, in registration order
- Detail
- Printed above 10 listeners for one event; nothing removed
'error'- Detail
- Thrown; stack trace printed; process exits unless the exception is caught
exec vs execFile- Detail
execspawns a shell;execFileruns the executable directly by default
Sources for this section (7)
- File systemnodejs.org (opens in a new tab)
- Pathnodejs.org (opens in a new tab)
- Modules: CommonJS modulesnodejs.org (opens in a new tab)
- Modules: ECMAScript modulesnodejs.org (opens in a new tab)
- HTTPnodejs.org (opens in a new tab)
- Eventsnodejs.org (opens in a new tab)
- Child processnodejs.org (opens in a new tab)
- Why does `await fs.readFile(file, 'utf8')` throw when `fs` is `require('node:fs')`?
- When does `path.resolve()` use the current working directory?
- Why should every long-lived `EventEmitter` have an `'error'` listener?
- Which two `child_process` functions return a command's stdout to a callback?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the JavaScript Developer pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 24 lessons of the JavaScript Developer guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (7 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams