DEV Community

Tarun Dudhatra
Tarun Dudhatra

Posted on Originally published at databeautifier.com

Top Node.js Interview Questions and Answers (2026)

Node.js powers the backend of millions of applications worldwide. If you're preparing for a backend or full-stack role, these Node.js interview questions cover everything from event loop fundamentals to production best practices.

📌 For the full list with detailed answers, visit: Node.js Interview Questions – DataBeautifier


1. What is Node.js and how does it work?

Node.js is a JavaScript runtime built on Chrome's V8 engine that allows JavaScript to run on the server-side.

Key characteristics:

  • Single-threaded — runs on one thread using an event loop
  • Non-blocking I/O — handles multiple requests without waiting for each to complete
  • Event-driven — uses callbacks, promises, and async/await for asynchronous operations
  • NPM ecosystem — largest package registry in the world

2. What is the Event Loop in Node.js?

The Event Loop is what allows Node.js to perform non-blocking operations despite being single-threaded.

Execution order (phases):

  1. timers — setTimeout, setInterval callbacks
  2. I/O callbacks — most async I/O callbacks
  3. idle/prepare — internal use
  4. poll — retrieve new I/O events
  5. check — setImmediate callbacks
  6. close callbacks — socket.on('close', ...)
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
console.log('4');
// Output: 1, 4, 3, 2
// Microtasks (Promises) run before the next event loop phase
Enter fullscreen mode Exit fullscreen mode

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

  • process.nextTick() — runs before the next event loop iteration (microtask queue)
  • setImmediate() — runs in the check phase of the event loop
setImmediate(() => console.log('setImmediate'));
process.nextTick(() => console.log('nextTick'));
// Output: nextTick → setImmediate
Enter fullscreen mode Exit fullscreen mode

Use process.nextTick() for tasks that must run before any I/O. Use setImmediate() for tasks that should run after I/O.


4. What is the difference between require() and import?

require() (CommonJS) import (ES Modules)
Syntax const x = require('x') import x from 'x'
Loading Synchronous Asynchronous
Dynamic import Yes import() function
Default in Node Yes (pre v12) Needs .mjs or "type":"module"

Node.js supports both, but CommonJS is still more common in backend code.


5. What is the Node.js cluster module?

Since Node.js is single-threaded, it can't use multiple CPU cores by default. The cluster module allows you to fork multiple worker processes that share the same port.

const cluster = require('cluster');
const os = require('os');

if (cluster.isMaster) {
  os.cpus().forEach(() => cluster.fork());
  cluster.on('exit', (worker) => {
    console.log(`Worker ${worker.process.pid} died. Forking...`);
    cluster.fork();
  });
} else {
  require('./server'); // each worker runs the server
}
Enter fullscreen mode Exit fullscreen mode

6. What is middleware in Express.js?

Middleware is a function that runs between the request and response cycle in Express.

// Middleware signature
function myMiddleware(req, res, next) {
  console.log(`${req.method} ${req.url}`);
  next(); // pass to next middleware
}

app.use(myMiddleware);
Enter fullscreen mode Exit fullscreen mode

Common uses: logging, authentication, body parsing, error handling, CORS.


7. What is the difference between readFileSync and readFile?

// Synchronous — BLOCKS the event loop (avoid in production)
const data = fs.readFileSync('file.txt', 'utf8');

// Asynchronous — non-blocking (preferred)
fs.readFile('file.txt', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});

// Modern: async/await with promises
const data = await fs.promises.readFile('file.txt', 'utf8');
Enter fullscreen mode Exit fullscreen mode

Always use async file operations in production to avoid blocking the event loop.


8. What are Streams in Node.js?

Streams allow processing data piece by piece (chunks) instead of loading it all into memory.

Types:

  • Readable — read data (fs.createReadStream)
  • Writable — write data (fs.createWriteStream)
  • Duplex — both (net.Socket)
  • Transform — modify data while streaming (zlib.createGzip)
// Stream a large file without loading it entirely into memory
const readable = fs.createReadStream('bigfile.csv');
const writable = fs.createWriteStream('output.csv');
readable.pipe(writable);
Enter fullscreen mode Exit fullscreen mode

9. How do you handle errors in async Node.js code?

// Callbacks — first arg is error
fs.readFile('file.txt', (err, data) => {
  if (err) return console.error(err);
  console.log(data);
});

// Promises
fetch('/api').then(r => r.json()).catch(err => console.error(err));

// async/await
async function getData() {
  try {
    const res = await fetch('/api');
    return await res.json();
  } catch (err) {
    console.error('Error:', err);
  }
}

// Global unhandled rejection handler (last resort)
process.on('unhandledRejection', (reason) => {
  console.error('Unhandled rejection:', reason);
});
Enter fullscreen mode Exit fullscreen mode

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

// exports is a shorthand reference to module.exports
exports.greet = () => console.log('Hello');

// module.exports replaces the entire exported object
module.exports = { greet: () => console.log('Hello') };

// WRONG — this breaks the reference
exports = { greet: () => {} }; // doesn't work!
Enter fullscreen mode Exit fullscreen mode

When you need to export a single class or function, always use module.exports.


Want the Full List?

This covers Node.js fundamentals. For the complete set including authentication, database integration, REST vs GraphQL, performance tuning, and deployment:

👉 Node.js Interview Questions & Answers – DataBeautifier

Covers beginner through senior-level questions with detailed explanations. Bookmark it! 🚀

Top comments (0)