Node.js event loop phases, modules, fs, path, streams, buffers, process, events and npm basics.
40 cards · basic cards · AI-written, checked twice. Edit anything.
- What is the Node.js event loop?
Show answer
The mechanism that allows Node.js to perform non-blocking I/O operations by delegating work to the system kernel and executing callbacks when operations complete.
- Name the six main phases of the Node.js event loop.
Show answer
timers, pending callbacks, idle/prepare, poll, check, close callbacks
- What happens in the timers phase of the event loop?
Show answer
Callbacks scheduled by setTimeout() and setInterval() are executed if their delay has expired.
- What happens in the pending callbacks phase of the event loop?
Show answer
Callbacks deferred to the next iteration of the event loop are executed, typically for I/O operations that failed.
- What happens in the idle/prepare phase of the event loop?
Show answer
Internal preparation occurs for the next phases; this phase is rarely encountered in user code.
- What happens in the poll phase of the event loop?
Show answer
The event loop retrieves new I/O events and executes their associated callbacks; it handles file system, network, and other async operations.
- What happens in the check phase of the event loop?
Show answer
Callbacks scheduled by setImmediate() are executed.
- What happens in the close callbacks phase of the event loop?
Show answer
Callbacks for closed connections and aborted operations are executed.
- What is a Node.js module?
Show answer
A reusable block of code whose existence does not accidentally impact other code; a file containing JavaScript functionality that can be used in other files.
- What does require() do?
Show answer
It loads a module and returns the module's exports so it can be used in the current file.
- What is module.exports?
Show answer
An object that defines what a module exports and makes it available to other modules that require() it.
- What is the difference between CommonJS and ES modules in Node.js?
Show answer
CommonJS uses require() and module.exports; ES modules use import and export statements. Node.js uses CommonJS by default unless .mjs extension or type field is specified.
- How do you export a function from a module?
Show answer
Assign the function to module.exports, for example: module.exports = function() { ... };
- What does fs.readFile() do?
Show answer
Reads the contents of a file asynchronously without blocking; requires a callback that receives the file data.
- What does fs.readFileSync() do?
Show answer
Reads the contents of a file synchronously and blocks execution until the read completes; returns the file data directly.