Advertisement

Core Node.js Modules interview questions and answers

15 Core Node.js Modules interview questions for Node.js roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Intermediate Save

What does the node:util.types module provide, and how does it differ from JavaScript's built-in typeof operator?

util.types provides precise type-checking functions (isArrayBuffer, isDate, isPromise, etc.) correctly distinguishing built-in object types -- something typeof can't do, since it returns 'object' for arrays, dates, regexes, and plain objects alike.
util.types.isDate(new Date()); // true
util.types.isPromise(Promise.resolve()); // true
Real-world example
A serialization library uses util.types.isDate() and isRegExp() to correctly detect these types during serialization, impossible to do reliably with typeof alone.
Likely follow-ups
  • How does util.types.isNativeError() differ from checking instanceof Error?
  • What's the relationship between util.types and Object.prototype.toString.call()?
Design Patterns in JavaScript Debugging & Diagnostics
Q12 Beginner Save

What is the difference between require() (CommonJS) and import (ES Modules) when loading Node.js's own core modules?

require() is synchronous, part of CommonJS; import is part of the ES Module standard, resolved as part of module loading, requiring either a .mjs extension or "type": "module" in package.json -- core modules support both, but a project must be consistent about which system its files use.
// CommonJS
const fs = require('node:fs');
// ES Modules
import fs from 'node:fs';
Real-world example
A team migrating to ES Modules sets "type": "module" in package.json and converts every require() to import, checking remaining CommonJS-only dependencies still load via Node's interop.
Likely follow-ups
  • What's the interop rule for a CommonJS file trying to import an ES Module?
  • Why does an ESM file need dynamic import() to conditionally load a CommonJS module?
Modules (CommonJS/ESM) CLI Tools & Scripting with Node.js
Q13 Intermediate Save

What does the node:readline module provide, and what is a common use case for it?

readline reads input from a readable stream (commonly process.stdin) line by line -- the standard way to build interactive command-line prompts, asking the user a question and waiting for their response without manually buffering stdin.
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
rl.question('What is your name? ', (name) => { console.log(`Hello, ${name}!`); rl.close(); });
Real-world example
A database migration CLI uses readline to prompt 'This will delete data, continue? (y/n)' before a destructive operation, giving the user a chance to cancel.
Likely follow-ups
  • How does readline/promises simplify usage with async/await compared to the callback-based version?
  • How would you build a multi-step interactive CLI wizard using repeated readline.question() calls?
CLI Tools & Scripting with Node.js Async Patterns
Q14 Advanced Save

What does the node:perf_hooks module provide, and how would you use it to measure precise execution time of a code block?

perf_hooks provides high-resolution timing via performance.now() (unaffected by system clock adjustments, unlike Date.now()) and PerformanceObserver for capturing marks and measures -- useful for precisely benchmarking operations without clock-drift imprecision.
performance.mark('start');
expensiveOperation();
performance.mark('end');
performance.measure('duration', 'start', 'end');
Real-world example
An API wraps a latency-sensitive query with performance.now() timestamps for its metrics system, using perf_hooks since Date.now()'s lower resolution would be less reliable for sub-millisecond differences.
Likely follow-ups
  • Why is performance.now() more reliable for timing than Date.now()?
  • How does PerformanceObserver compare to simply subtracting two performance.now() timestamps?
Performance Optimization & Profiling Debugging & Diagnostics
Q15 Beginner Save

What does the node:events module's EventEmitter class provide, and what naming convention do most Node.js core APIs follow when using it?

EventEmitter enables the publish/subscribe pattern -- objects extending it emit named events via .emit(), subscribed via .on() or .once(). Core APIs built on streams and servers follow lowercase, camelCase event names like 'data', 'error', 'close', a convention userland libraries typically follow too.
const emitter = new EventEmitter();
emitter.once('ready', () => console.log('Ready fired once'));
emitter.emit('ready');
Real-world example
A job-processing library built on EventEmitter emits 'started', 'progress', and 'completed' events, following familiar conventions that make the API immediately intuitive to Node.js developers.
Likely follow-ups
  • What happens if an EventEmitter emits an 'error' event with no listener attached?
  • How does EventEmitter's default max-listener limit (10) work, and when would you raise it?
Events & EventEmitter Design Patterns in JavaScript

Showing 11–15 of 15