Top Node.js Interview Questions and Answers
The Node.js interview questions asked most often, covering the event loop, libuv, streams, Express middleware, error handling and scaling.
Backend and full-stack interviews almost always include Node.js. These questions cover how Node works under the hood, how it handles concurrency, and the Express patterns you'll use every day.
1. What is Node.js and why is it fast for I/O?#
Node.js is a JavaScript runtime built on Chrome's V8 engine. It uses a single-threaded, non-blocking, event-driven model. Instead of waiting for a file read or database query, Node registers a callback and moves on, so one process can handle thousands of connections at once.
2. If Node is single-threaded, how does it handle async work?#
Your JavaScript runs on one thread, but Node uses libuv underneath. Network I/O is handled by the operating system, and tasks like file system access, DNS lookups and some crypto run on libuv's thread pool (4 threads by default). When they finish, their callbacks are queued for the event loop.
3. What are the phases of the Node.js event loop?#
- Timers: runs
setTimeoutandsetIntervalcallbacks. - Pending callbacks: some deferred system callbacks.
- Poll: waits for and runs I/O callbacks.
- Check: runs
setImmediatecallbacks. - Close callbacks: such as
socket.on("close").
Between phases, Node runs process.nextTick callbacks first, then promise microtasks.
4. process.nextTick vs setImmediate vs setTimeout#
setTimeout(() => console.log("timeout"), 0);
setImmediate(() => console.log("immediate"));
process.nextTick(() => console.log("nextTick"));
Promise.resolve().then(() => console.log("promise"));
// nextTick, promise, then timeout and immediate
// (the order of the last two can vary in the main module)Inside an I/O callback, setImmediate always runs before setTimeout(..., 0).
5. What is middleware in Express?#
Middleware is a function that runs between the request and the response. It can read or change req and res, end the request, or call next() to pass control on. It's used for logging, authentication, parsing bodies and error handling.
function auth(req, res, next) {
if (!req.headers.authorization) {
return res.status(401).json({ error: "Unauthorized" });
}
next();
}
app.get("/profile", auth, (req, res) => {
res.json({ name: "Asha" });
});6. How do you handle errors in Express?#
Add an error-handling middleware with four arguments, registered after your routes. In Express 5, errors thrown in async route handlers are passed to it automatically. In Express 4, catch them and call next(err).
app.use((err, req, res, next) => {
console.error(err);
res.status(err.status || 500).json({ error: "Something went wrong" });
});7. What are streams?#
Streams process data piece by piece instead of loading it all into memory. There are four types: Readable, Writable, Duplex and Transform. They're ideal for large files, video and uploads.
// ES module (top-level await works here)
import fs from "node:fs";
import zlib from "node:zlib";
import { pipeline } from "node:stream/promises";
await pipeline(
fs.createReadStream("big.log"),
zlib.createGzip(),
fs.createWriteStream("big.log.gz")
);8. How do you run CPU-heavy work without blocking Node?#
CPU-heavy code (image processing, large calculations) blocks the event loop and freezes every request. Move it to worker threads, a separate process with child_process, or a job queue. Use the cluster module or a process manager like PM2 to run one Node process per CPU core.
9. CommonJS vs ES modules#
CommonJS uses require and module.exports and loads synchronously. ES modules use import and export, are the JavaScript standard, and support top-level await. Enable ES modules with "type": "module" in package.json or the .mjs extension.
Saying Node is "multi-threaded because of the thread pool." Your JavaScript still runs on one thread. Only certain native operations use the pool.
Key takeaways#
- Explain non-blocking I/O and the role of libuv clearly.
- Know the event loop phases and where nextTick and promises fit.
- Never block the event loop. Know your options for CPU-heavy work.