Interview questions & answers

Node.js interview questions & answers

What are the most common Node.js interview questions?

Node.js is a runtime that executes JavaScript outside the browser using Google's V8 engine, designed for building fast, scalable network applications. It uses a single-threaded, event-driven, non-blocking I/O model that handles many concurrent connections efficiently. Interviews test the event loop, asynchronous patterns, streams, the module system, and how Node achieves concurrency without multiple threads per request.

Updated 2026-06-18 · 15 real, commonly-asked questions with answers.

Key takeaways

  • Node.js is a runtime that executes JavaScript outside the browser using Google's V8 engine, designed for building fast, scalable network applications.
  • Core areas to revise for Node.js: Event loop & non-blocking I/O, Asynchronous patterns, Streams & buffers, Modules (CommonJS & ESM), Express & middleware.
  • This guide answers 15 of the most-asked Node.js interview questions — rehearse them in OnJob's free AI mock interview.
Event loop & non-blocking I/OAsynchronous patternsStreams & buffersModules (CommonJS & ESM)Express & middlewareError handlingEventEmitternpm & package management

Top 15 Node.js interview questions

Q1.What is Node.js and what is it used for?

Node.js is a JavaScript runtime built on the V8 engine that lets you run JavaScript on the server. Its non-blocking, event-driven architecture makes it well suited for I/O-heavy applications like REST APIs, real-time apps (chat, streaming), and microservices. It is less ideal for CPU-intensive tasks, which can block its single event-loop thread.

Q2.How does the Node.js event loop work?

The event loop is what lets single-threaded Node perform non-blocking I/O by offloading operations (file, network) to the system and registering callbacks. It runs in phases (timers, pending callbacks, poll, check, close) and processes the callbacks queued in each phase. When an async operation completes, its callback is queued and executed when the loop reaches the right phase.

Q3.Is Node.js single-threaded? How does it handle concurrency?

Node's JavaScript execution and event loop run on a single thread, but Node is not entirely single-threaded: a background libuv thread pool handles certain operations like file system and some crypto work. Concurrency comes from non-blocking I/O, where Node initiates an operation and continues processing other events instead of waiting, then runs the callback when the operation finishes.

Q4.What is the difference between blocking and non-blocking code?

Blocking code halts the execution of further JavaScript until the operation completes, such as the synchronous fs.readFileSync. Non-blocking code initiates an operation and returns immediately, running a callback (or resolving a promise) when it finishes, such as fs.readFile. In Node you favor non-blocking calls so the single thread stays free to handle other requests.

Q5.What is the difference between process.nextTick and setImmediate?

process.nextTick queues a callback to run immediately after the current operation completes, before the event loop continues to the next phase, so it runs sooner. setImmediate queues a callback to run in the check phase of the next event-loop iteration. Overusing process.nextTick can starve the event loop because its queue is drained before I/O.

Q6.What are streams in Node.js?

Streams are objects that let you read or write data piece by piece instead of loading it all into memory, which is essential for large files or network data. The four types are Readable, Writable, Duplex, and Transform. You can pipe a readable stream into a writable one to process data efficiently with low memory usage.

Q7.What is the difference between CommonJS and ES modules?

CommonJS is Node's original module system using require() to import and module.exports to export, loaded synchronously, with files typically ending in .js or .cjs. ES Modules (ESM) use the standard import and export syntax, are loaded asynchronously, and use .mjs or are enabled via type: module in package.json. ESM is the JavaScript standard; Node supports both.

Q8.What is middleware in Express?

Middleware is a function that has access to the request and response objects and the next function in the request-response cycle. It can run code, modify the request or response, end the cycle, or call next() to pass control to the next middleware. It is used for logging, authentication, body parsing, error handling, and more, executed in the order they are registered.

Q9.What is the purpose of package.json?

package.json is the manifest of a Node project. It records metadata (name, version), lists dependencies and devDependencies with version ranges, defines runnable scripts (like start and test), and specifies the entry point and module type. Running npm install reads it to install the listed packages, making builds reproducible.

Q10.What is the difference between dependencies and devDependencies?

dependencies are packages your application needs to run in production, such as a web framework. devDependencies are packages needed only during development and testing, such as test runners, linters, and build tools. When you install with a production flag (npm install --production), devDependencies are skipped, keeping the production install lean.

Q11.How do you handle errors in asynchronous Node code?

For promises and async/await, wrap awaited calls in try/catch and always attach a catch to promise chains. For callback-style APIs, follow the error-first convention where the first callback argument is an error to check. For streams and event emitters, listen for the error event. Unhandled rejections and uncaught exceptions should be logged and handled at the process level as a last resort.

Q12.What is the difference between exports and module.exports?

module.exports is the actual object that a module returns when required. exports is just a convenience reference pointing to the same object initially, so you can attach properties to it. If you reassign exports to a new value it breaks that link and exports nothing useful; to export a single value you must assign to module.exports directly.

Q13.What is the event emitter pattern in Node?

Node's EventEmitter class lets objects emit named events and register listener functions that run when those events fire, implementing the observer pattern. You call emitter.on(event, listener) to subscribe and emitter.emit(event, data) to trigger. Many core modules (like streams and HTTP servers) are built on EventEmitter, making event-driven programming central to Node.

Q14.What is the cluster module and why use it?

Because Node runs JavaScript on a single thread, it cannot use multiple CPU cores by default. The cluster module lets you fork multiple worker processes that share the same server port, distributing incoming connections across them so the app uses all available cores. This improves throughput and resilience, since one crashing worker does not take down the others.

Q15.What is npm and what does package-lock.json do?

npm is Node's default package manager, used to install, update, and manage third-party packages from the npm registry. package-lock.json records the exact resolved version of every installed package and its dependencies, ensuring that everyone who installs the project gets an identical dependency tree. It makes installs deterministic and reproducible across machines.

Free AI mock interview

Practise Node.js out loud

Reading answers is step one. Rehearse them in OnJob's free AI mock interview, get instant feedback, then apply to AI-matched jobs in one click.

Create my free profile — free