AI
prepair.app
Start interview →
EnglishРусскийУкраїнська
🟢

Node.js interview questions

Node.js interviews revolve around the event loop, asynchronous patterns, Express or NestJS, and the consequences of a single-threaded runtime. Below are the most common questions with model answers.

Junior · no experience / under 1 yearMiddle · 2–4 years of experienceSenior · 5+ years of experience

What they ask about

Event loop and its phases
Promises and async/await
Streams and buffers
Express / NestJS
Middleware and error handling
JWT and authentication

10 real questions with answers

Every question comes with a model answer you can compare yours against.

1

Explain the event loop in Node.js. What are its phases?

Answer

The event loop repeatedly processes queues of completed operations in phases: timers for setTimeout and setInterval, pending callbacks, poll for incoming I/O, check for setImmediate, and close callbacks. Between every phase Node drains the microtask queue — process.nextTick first, then promise callbacks. This is why a long synchronous function blocks every request on the server: nothing else can run until it returns.

2

What is the difference between process.nextTick() and setImmediate()?

Answer

process.nextTick queues a callback to run before the event loop continues to the next phase, ahead of promise microtasks. setImmediate schedules the callback for the check phase of the next iteration. nextTick therefore always runs sooner, and recursive nextTick calls can starve the event loop entirely, which is why setImmediate is the safer default for deferring work.

3

What is a Promise? Explain Promise.all versus Promise.allSettled.

Answer

A Promise represents a value that will be available later and is either pending, fulfilled, or rejected. Promise.all resolves when every input resolves but rejects immediately on the first rejection, discarding the other results — right when you need all of them to proceed. Promise.allSettled always resolves with a status and value for each entry, which suits independent operations where one failure should not cancel the rest.

4

How do you handle errors in async/await? What happens on an unhandled rejection?

Answer

You wrap awaited calls in try/catch, or attach .catch to the returned promise. In Express, an error thrown inside an async handler is not caught by the framework unless you forward it to next(), so most teams use a wrapper or express-async-errors. An unhandled rejection terminates the process by default in modern Node, which is intentional — the process is in an unknown state and should be restarted.

5

What are streams and when do you use them instead of reading a whole file?

Answer

Streams process data in chunks, so memory usage stays constant regardless of payload size and the first byte can be sent before the last is read. Reading a two-gigabyte file into a Buffer exhausts memory and delays the response; piping it streams straight to the client. Streams also compose through pipe or pipeline, letting you chain decompression, transformation, and output with backpressure handled for you.

6

What is middleware in Express? How does next() work?

Answer

Middleware is a function receiving request, response, and next, executed in registration order to form a pipeline. Calling next() hands control to the next middleware; not calling it and not sending a response leaves the request hanging. Passing an argument to next() skips ahead to error-handling middleware, which is recognised by having four parameters.

7

How does Node handle CPU-intensive tasks? What are worker threads?

Answer

Because JavaScript runs on one thread, a CPU-heavy computation blocks every pending request. Worker threads run JavaScript in parallel threads within the same process, communicating by message passing and shared array buffers, which suits hashing, image processing, or parsing. The alternatives are offloading to a separate service or a job queue — clustering only adds processes and does not make a single request faster.

8

What is a JWT? Where do you store it and what are the risks?

Answer

A JWT is a signed token carrying claims, so the server can authenticate a request without a session lookup. Storing it in localStorage exposes it to XSS; storing it in an httpOnly, Secure, SameSite cookie protects against XSS but requires CSRF defence. The deeper trade-off is revocation: a stateless token stays valid until it expires, so sensitive systems use short-lived access tokens with refresh tokens and a server-side denylist.

9

What is the difference between require and import? What are CommonJS and ESM?

Answer

CommonJS require is synchronous, resolved at runtime, and can be called conditionally inside a function. ESM import is static, hoisted, and analysed before execution, which enables tree shaking and top-level await. A package declares its format with "type": "module" or the .mjs and .cjs extensions; ESM can import CommonJS, but the reverse needs a dynamic import() because require cannot load an async module graph.

10

How do you implement a graceful shutdown?

Answer

On SIGTERM you stop accepting new connections with server.close(), let in-flight requests finish, then close database pools, message consumers, and timers before exiting. A timeout forces exit if something hangs, so the container is never stuck. Without this, a deploy kills requests mid-flight and can leave transactions or queue messages in an inconsistent state.

🦎

Reading answers is not enough

In a real interview you speak under pressure. Cam asks these same questions, scores every answer, and shows exactly what to fix.

Practice a Node.js Backend interview →
Free · 3 interviews per month

Other specializations

🔍Manual QA🤖QA AutomationJava Backend🐍Python Backend🐘PHP Backend🦫Go Backend⚛️React Frontend💚Vue Frontend🅰️Angular FrontendNext.js📈Business Analyst🎯Product Manager📋Project Manager🎨UI/UX Designer📣Marketing