Node.js Interview Questions and Answers
Node.js interviews focus on how a single-threaded runtime handles many concurrent requests and what can go wrong when you block it. Beginners are asked about modules, npm and Express; mid-level candidates about the event loop phases, streams and authentication; senior candidates about scaling, clustering and keeping the event loop healthy.
Usually asked in backend and full stack interviews, from intern / fresher to senior (5+ years).
Intern / Fresher
- Q1Intern / FresherWhat is Node.js and why is it called single-threaded?
Node.js is a JavaScript runtime built on Chrome's V8 engine that runs your JavaScript on a single main thread and uses non-blocking I/O, so one thread can handle many concurrent connections.
- Q2Intern / FresherWhat is the difference between package.json and package-lock.json?
package.json lists your project's dependencies with allowed version ranges, while package-lock.json records the exact versions that were installed so every machine gets an identical dependency tree.
Junior (0-2 years)
- Q1Junior (0-2 years)What is the difference between require and import in Node.js?
require is the CommonJS module system, which loads modules synchronously at runtime, while import is the ES module standard, which is statically analysed, asynchronous and supports top-level await and tree shaking.
- Q2Junior (0-2 years)What is middleware in Express and how does it work?
Middleware are functions that receive the request, response and a next callback; they run in the order they are registered and can modify the request, end the response or pass control on with next().
Mid-level (2-5 years)
- Q1Mid-level (2-5 years)What is the difference between process.nextTick, setImmediate and setTimeout in Node.js?
process.nextTick runs its callback right after the current operation, before the event loop continues; setTimeout runs in the timers phase after a minimum delay; and setImmediate runs in the check phase right after the poll (I/O) phase.
- Q2Mid-level (2-5 years)What are streams in Node.js and why should you use them?
Streams process data piece by piece as it arrives instead of loading it all into memory, which lets you handle large files and network data efficiently; they come in Readable, Writable, Duplex and Transform types.
- Q3Mid-level (2-5 years)What is the difference between JWT and session-based authentication?
Session authentication stores the login state on the server and gives the browser a session ID cookie, while JWT authentication gives the client a signed token that contains the user's claims so the server can verify it without storing state.
- Q4Mid-level (2-5 years)How do you secure a Node.js Express API?
Validate all input, use parameterised queries, add security headers with helmet, rate-limit requests, restrict CORS, hash passwords, keep secrets in environment variables, and keep dependencies updated and audited.
Senior (5+ years)
- Q1Senior (5+ years)How do you scale a Node.js application and avoid blocking the event loop?
Keep the main thread free by moving CPU-heavy work to worker threads or a job queue, and scale across cores and machines by running multiple processes (cluster or PM2) behind a load balancer with shared, external state.