Skip to content
SWENG.dev
Go back

Node.js Interview

Edit page

Most Probable Node.js Interview Questions (2025) — with thorough answers

Curated and ordered by likelihood based on common interview patterns for React engineers in 2024–2025. Answers are practical, detailed, and include pitfalls, examples, and “when to use” guidance.


1) Explain Node.js’s event-driven, non-blocking I/O architecture

Node.js uses an event-driven, non-blocking I/O model. This means operations like reading files, network requests, or database queries do not block the main thread. Instead, Node.js delegates these tasks to the system and continues executing other code. When the operation completes, a callback is triggered.

Benefits:

Pitfall:

Example:

fs.readFile('file.txt', (err, data) => {
  if (err) throw err;
  console.log(data);
});
// Code below runs immediately, not after file is read

2) What is the Node.js event loop and how does it work?

The event loop is the core mechanism that allows Node.js to perform non-blocking I/O. It continuously checks for pending events (timers, I/O, callbacks) and processes them in phases. When your code schedules async work (e.g., setTimeout, fs.readFile), the event loop ensures callbacks run when the work is done.

Phases:

Example:

setTimeout(() => console.log('timeout'), 0);
setImmediate(() => console.log('immediate'));
console.log('sync');
// Output order: sync, timeout OR immediate (order not guaranteed)

3) What is the difference between asynchronous and synchronous programming?

Example:

// Synchronous
const data = fs.readFileSync('file.txt');
console.log(data);

// Asynchronous
fs.readFile('file.txt', (err, data) => {
  console.log(data);
});
console.log('This runs before file is read');

4) How does Node.js handle multiple requests if it’s single-threaded?

Node.js runs JavaScript in a single thread, but uses background threads (via libuv) for I/O. When a request comes in, Node.js delegates I/O work to the system, freeing the main thread to handle more requests. Callbacks are queued and processed by the event loop.

Key point:


5) What happens if you run CPU-heavy code in Node.js?

CPU-heavy code (e.g., image processing, large calculations) blocks the event loop, preventing Node.js from handling other requests. This leads to poor performance and unresponsive servers.

Solution:

Example:

// Bad: blocks event loop
while (true) {
  /* infinite loop */
}

// Good: use worker_threads
const { Worker } = require('worker_threads');
new Worker('./heavy-task.js');

6) What is the EventEmitter class?

EventEmitter is a core Node.js class that enables objects to emit and listen for named events. It’s the foundation for many Node.js APIs (e.g., streams, HTTP server).

Example:

const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('greet', (name) => console.log(`Hello, ${name}`));
emitter.emit('greet', 'Alice');

7) Explain middleware in Express.js

Middleware are functions that run during the request-response cycle in Express. They can modify the request/response, end the request, or pass control to the next middleware.

Types:

Example:

app.use((req, res, next) => {
  console.log('Request received');
  next();
});

8) How do you scale Node.js applications?

Example:

const cluster = require('cluster');
if (cluster.isMaster) {
  for (let i = 0; i < require('os').cpus().length; i++) {
    cluster.fork();
  }
} else {
  // Worker process
  require('./server');
}

9) What is the V8 engine?

V8 is Google’s open-source JavaScript engine, written in C++. It compiles JavaScript to machine code for fast execution. Node.js uses V8 to run JavaScript on the server.

Key features:


10) What are callbacks and what is callback hell?

Solution:

Example:

// Callback hell
fs.readFile('a.txt', (err, a) => {
  fs.readFile('b.txt', (err, b) => {
    fs.readFile('c.txt', (err, c) => {
      // ...
    });
  });
});

// Promises
fs.promises
  .readFile('a.txt')
  .then((a) => fs.promises.readFile('b.txt'))
  .then((b) => fs.promises.readFile('c.txt'));

11) What are Streams in Node.js?

Streams are objects for reading or writing data piece by piece (chunks), rather than all at once. Useful for large files, network sockets, etc.

Types:

Example:

const fs = require('fs');
const readStream = fs.createReadStream('file.txt');
readStream.on('data', (chunk) => console.log(chunk));

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

Example:

process.nextTick(() => console.log('nextTick'));
setImmediate(() => console.log('immediate'));
console.log('sync');
// Output: sync, nextTick, immediate

13) What are global objects in Node.js?

Global objects are available in all Node.js modules without requiring imports. Examples: global, process, __dirname, __filename, Buffer, setTimeout, etc.

Pitfall:


14) What is the cluster module and how does it enable multi-threading?

The cluster module allows Node.js to create child processes (workers) that share the same server port. Each worker runs in its own process, enabling multi-core CPU usage. Communication is via IPC (inter-process communication).

Example:

const cluster = require('cluster');
if (cluster.isMaster) {
  cluster.fork();
} else {
  // Worker code
}

15) How do you prevent memory leaks in Node.js?

Example:

emitter.removeAllListeners();
global.bigObject = null;

16) What are the security best practices for Node.js applications?


17) How do you debug Node.js applications?

Example:

// Start with inspect
node --inspect-brk app.js
// Open chrome://inspect in Chrome


Edit page
Share this post on:

Previous Post
I Learned More by Arguing With AI Than Watching Tutorials
Next Post
React Interview