Node.js Interview Questions - NodeJS Node JS Interview Prep
50 Questions Available
Comprehensive collection of 50 essential Node.js interview questions covering event loop, async programming, modules, streams, and backend concepts. Free Node.js, NodeJS, Node JS interview questions with answers. Node.js interview prep guide.
All Questions & Answers
What is Node.js and what are its main features?
Node.js is a JavaScript runtime built on Chrome's V8 engine that allows running JavaScript on the server. Main features include non-blocking I/O, event-driven architecture, single-threaded event loop, npm package ecosystem, and ability to build scalable network applications.
Explain the Node.js event loop and how it works.
The event loop is Node.js's mechanism for handling asynchronous operations. It has phases: timers (setTimeout/setInterval), pending callbacks, idle/prepare, poll (I/O operations), check (setImmediate), and close callbacks. It continuously processes these phases, executing callbacks when operations complete.
What is the difference between process.nextTick() and setImmediate()?
process.nextTick() executes before the event loop continues to the next phase, with highest priority. setImmediate() executes in the check phase of the next event loop iteration. Despite names, nextTick runs before setImmediate.
How does Node.js handle blocking operations?
Node.js offloads blocking operations to libuv thread pool (default 4 threads) or system kernel. Operations like file I/O, DNS lookups use thread pool, while network I/O uses kernel async mechanisms. CPU-intensive tasks should use worker threads or child processes.
What is the difference between require() and import?
require() is CommonJS (synchronous, used in Node.js), loads modules at runtime. import is ES6 modules (asynchronous, can be used with .mjs or type: "module"), supports static analysis, tree-shaking, and top-level await. CommonJS uses module.exports, ES6 uses export.
Explain callback hell and how to avoid it.
Callback hell occurs when multiple nested callbacks make code hard to read. Avoid it using promises, async/await, named functions, or libraries like async.js. Promises and async/await provide cleaner error handling and flow control.
What are Promises and how do they work?
Promises represent eventual completion of async operations. States: pending, fulfilled, rejected. They provide .then() for success, .catch() for errors, .finally() for cleanup. Promises chain and can be used with async/await for cleaner code.
What is async/await and how does it work?
async/await is syntactic sugar over promises. async functions return promises. await pauses execution until promise resolves. Errors are caught with try/catch. Makes asynchronous code look synchronous and easier to read.
What is the difference between spawn, exec, and fork in child_process?
spawn launches process, returns stream, non-blocking. exec runs command in shell, buffers output, has size limit. fork is spawn for Node.js processes, enables IPC. Use spawn for large output, exec for simple commands, fork for Node.js workers.
What are streams in Node.js and their types?
Streams handle data in chunks instead of loading all at once. Types: Readable (data source), Writable (data destination), Duplex (both), Transform (duplex with modification). Benefits: memory efficient, composable, handle backpressure. Examples: fs.createReadStream, HTTP requests.