DevAcademy
All interview questions
26+ Questions

Node.js Interview Questions & Answers

Curated Node.js interview questions with detailed answers, grouped by topic. Search, revise and get interview-ready.

Filter by difficulty:

Node.js Introduction

Q1. What is Node.js, and what engine does it run on?

A JavaScript runtime, built on Chrome's V8 engine, that lets JavaScript run outside a browser — on a server, as a CLI tool, or in a build script.

Q2. What is a key difference between Node.js and browser JavaScript environments?

Node.js provides server-side APIs like file system and networking access instead of browser APIs like the DOM and window object — the core JavaScript language itself is the same in both.

Node.js Architecture

Q3. Is Node.js single-threaded or multi-threaded for running JavaScript?

IntermediateLearn topic →

Single-threaded for executing your JavaScript code, but it delegates I/O work to libuv's thread pool and OS-level async mechanisms, so it can still handle many concurrent operations without blocking.

Q4. Why is Node.js a poor fit for CPU-intensive synchronous work?

A CPU-heavy task runs on the single main thread and blocks the event loop, delaying every other request until it finishes — worker threads exist specifically to move CPU-bound work off the main thread.

npm Basics

Q5. What is the purpose of package-lock.json?

IntermediateLearn topic →

It records the exact resolved version of every dependency (including nested ones), ensuring consistent installs across machines and CI.

Modules: CommonJS vs ESM

Q6. What is the main syntactic difference between CommonJS and ES Modules?

CommonJS uses require() and module.exports; ES Modules use import and export. A file uses one system or the other, not a mix.

Q7. How does Node.js decide whether a .js file is CommonJS or an ES Module?

By the "type" field in package.json ("module" for ESM, otherwise CommonJS by default), unless the file explicitly uses a .cjs or .mjs extension, which always overrides that setting.

The File System (fs) Module

Q8. Why should readFileSync() generally be avoided in a running web server?

IntermediateLearn topic →

It blocks the entire event loop until the disk read completes, freezing every other in-flight request in the meantime.

Streams

Q9. What problem do streams solve compared to reading an entire file into memory?

IntermediateLearn topic →

They process data in small chunks as it arrives, keeping memory usage low regardless of the total file size.

Q10. What is backpressure in the context of streams?

An automatic mechanism where .pipe() pauses a fast readable stream until a slower writable stream catches up, preventing unbounded memory growth.

The Event Loop in Node.js

Q11. What runs first: synchronous code, process.nextTick(), resolved Promise callbacks, or a setTimeout(fn, 0) callback?

Synchronous code runs first, then process.nextTick() callbacks, then resolved Promise (microtask) callbacks, and only then does the event loop reach the timers phase for setTimeout.

The process Object

Q12. Why should a server listen for the SIGTERM signal?

To shut down gracefully — finishing in-flight requests and closing database connections — before the process actually exits, which matters especially in containerized deployments.

The http Module

Q13. What does a framework like Express add on top of the core http module?

IntermediateLearn topic →

Routing, middleware, and convenient request/response helpers — none of which exist in the raw http module by default.

Middleware

Q14. What must an Express middleware function do to pass control to the next one?

Call next() — or send a response directly. Failing to do either leaves the request hanging indefinitely.

Query Params & Body Parsing

Q15. Why is req.body undefined even when a client sends a valid JSON body?

IntermediateLearn topic →

Because the express.json() (or similar) middleware was not registered before the route — without it, Express does not parse the body automatically.

Error Handling in Express

Q16. How does Express identify an error-handling middleware?

IntermediateLearn topic →

By its signature — a function with exactly four parameters: (err, req, res, next).

Q17. In older Express versions, does an unhandled rejected Promise in an async route handler automatically reach the error middleware?

No — it must be caught and passed explicitly via next(err), or wrapped in a helper that does so automatically (Express 5 changes this behavior).

REST API Design

Q18. What is the conventional difference between PUT and PATCH?

IntermediateLearn topic →

PUT conventionally replaces the entire resource; PATCH updates only the fields provided in the request.

Connecting to SQL

Q19. Why are parameterized queries important when querying a SQL database from Node.js?

They prevent SQL injection by safely escaping user-supplied values, instead of concatenating raw input directly into a query string.

Authentication Basics

Q20. What is the difference between authentication and authorization?

Authentication verifies who a user is; authorization determines what an already-authenticated user is allowed to do.

JWT (JSON Web Tokens)

Q21. Is the payload of a JWT encrypted?

IntermediateLearn topic →

No — it is only base64-encoded and readable by anyone who has the token. The signature only prevents undetected tampering, not reading the contents.

Q22. What is a downside of stateless JWT authentication compared to server-side sessions?

A JWT cannot be instantly revoked before it expires without additional infrastructure, like a token blocklist — unlike a database-backed session, which can be deleted immediately.

Password Hashing

Q23. Why is bcrypt preferred over a fast general-purpose hash like SHA-256 for passwords?

bcrypt is deliberately slow and includes an automatic per-user salt, making brute-force attacks impractical — a fast hash function lets attackers try billions of guesses per second.

CORS & Security Headers

Q24. Does CORS protect an API from all unauthorized access?

No — CORS is enforced by browsers for JavaScript-initiated cross-origin requests specifically. It does not stop server-to-server requests, curl, or non-browser clients.

Testing Node.js Apps

Q25. Why is it common to separate app.js from server.js in an Express project?

So tests can import the configured Express app directly using a tool like supertest, without needing to start a real server bound to a port.

Node.js Best Practices

Q26. What can happen if an unhandled promise rejection is left unaddressed in a Node.js app?

It can silently fail or, in newer Node.js versions, crash the process entirely — a process.on('unhandledRejection') handler helps surface these bugs during development.