Top 50 Node.
js Interview Questions with Precise Answers and Examples
1. What is [Link]?\ [Link] is a runtime environment that allows executing JavaScript outside the browser.
Built on Chrome's V8 engine, it is designed for building scalable, fast, and non-blocking server-side
applications.\ Example: Running a server with [Link].
[Link]('[Link] is running');
2. Is [Link] single-threaded?\ Yes, [Link] is single-threaded for event handling but uses a thread pool
via libuv to manage asynchronous tasks like file operations or DNS lookups.
3. Difference between [Link] and JavaScript?\ JavaScript is a language; [Link] is a runtime that lets you
run JavaScript on the server-side.
4. Why use [Link]?\ It provides event-driven, non-blocking architecture ideal for real-time apps, APIs, and
scalable network systems.
5. What is V8 engine?\ It is Google's open-source JavaScript engine that compiles JavaScript directly to
machine code, making execution fast.
6. What is the Event Loop in [Link]?\ The Event Loop handles asynchronous operations in [Link],
allowing non-blocking execution by offloading tasks and executing callbacks.
7. Difference between [Link]() and setImmediate()?
• [Link]() executes before the next event loop iteration.
• setImmediate() runs after the current poll phase of the event loop.
8. What is non-blocking I/O?\ Non-blocking I/O allows the application to continue running other tasks
while waiting for I/O operations to complete.
9. What is callback hell?\ Deeply nested callbacks that make code unreadable and hard to maintain.
Avoided using Promises or async/await.
10. How to avoid callback hell?\ Using Promises or async/await to flatten the structure.
async function fetchData() {
let data = await getData();
}
11. What is a module in [Link]?\ A module is a reusable block of code exported using [Link]
and imported using require() .
1
12. What is NPM?\ Node Package Manager, used to manage [Link] packages and project dependencies.
13. Difference between require() and import?\ require() is CommonJS syntax; import is ES6 syntax,
typically used in modern projects or with transpilers.
14. What is [Link]?\ A JSON file storing project metadata, dependencies, and scripts.
15. How to create a module?
// [Link]
[Link] = { add: (a, b) => a + b };
16. What is EventEmitter?\ A class allowing objects to emit and listen for events.
const EventEmitter = require('events');
const emitter = new EventEmitter();
[Link]('start', () => [Link]('Started'));
[Link]('start');
17. What are streams in [Link]?\ Streams handle data in chunks, improving efficiency for large data.
Types: Readable, Writable, Duplex, Transform.
18. What is buffer in [Link]?\ A temporary memory area for storing binary data, used with streams.
19. How to handle uncaught exceptions?
[Link]('uncaughtException', (err) => [Link](err));
20. Difference between readFile and createReadStream?\ readFile loads entire file in memory;
createReadStream reads data in chunks.
21. How to read a file asynchronously?
const fs = require('fs');
[Link]('[Link]', 'utf8', (err, data) => [Link](data));
22. How to create an HTTP server?
const http = require('http');
[Link]((req, res) => [Link]('Hello')).listen(3000);
2
23. What is middleware?\ Functions that execute during the request-response cycle, widely used in
[Link].
24. What is cluster module?\ Allows creating multiple [Link] processes to utilize multi-core CPUs.
25. What is non-blocking code?\ Code that doesn't block the main thread; other operations continue while
tasks complete in the background.
26. What are Promises?\ Objects representing eventual completion/failure of asynchronous operations.
27. What is async/await?\ Syntax simplifying asynchronous code by allowing await inside async
functions.
28. Difference between synchronous and asynchronous code?
• Synchronous blocks code execution.
• Asynchronous allows tasks to run in parallel.
29. What is process in [Link]?\ An object representing the running [Link] process, giving control and
information.
30. What is REPL?\ Read-Eval-Print Loop, an interactive shell for [Link].
31. What is [Link]?\ A lightweight [Link] framework for building web servers and APIs.
32. How to install Express?
npm install express
33. How to create a basic Express server?
const express = require('express');
const app = express();
[Link]('/', (req, res) => [Link]('Hello'));
[Link](3000);
34. What is routing in Express?\ Defining URL paths to handle client requests.
35. What are middlewares in Express?\ Functions with access to request, response, and next middleware.
36. How to prevent callback hell?\ Use Promises, async/await, or modular code structure.
37. How to handle errors properly?\ Using try-catch , centralized error handlers, and .catch() with
Promises.
3
38. How to secure a [Link] app?\ Input validation, [Link] for headers, rate limiting, and avoiding
injection attacks.
39. What is Helmet in [Link]?\ Middleware that secures HTTP headers.
40. What is CORS?\ Cross-Origin Resource Sharing, allows or restricts resource access from different
domains.
41. How to enable CORS in Express?
const cors = require('cors');
[Link](cors());
42. What is the difference between [Link]() and [Link]()?\ [Link]() terminates the
current process; [Link]() sends a signal to terminate.
43. How to debug [Link]?\ Using console logs, Node Inspector, Chrome DevTools, or VSCode debugger.
44. What is load balancing in [Link]?\ Distributing traffic across multiple processes for better scalability.
45. What is spawn() vs exec()?
• spawn() streams large outputs.
• exec() buffers output, good for small tasks.
46. What is middleware chaining?\ Multiple middleware functions executed in sequence.
47. What is difference between fork and cluster?\ fork() creates child processes; cluster uses
fork() to create multiple server instances.
48. What is [Link]?\ Object storing environment variables.
49. How to increase max listeners in EventEmitter?
[Link](20);
50. How to handle unhandled Promise rejections?
[Link]('unhandledRejection', (err) => [Link](err));