Nodejs Interview Questions
Nodejs Interview Questions
1. What is Node.js?\ Node.js 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 Node.js.
console.log('Node.js is running');
2. Is Node.js single-threaded?\ Yes, Node.js 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 Node.js and JavaScript?\ JavaScript is a language; Node.js is a runtime that lets you
run JavaScript on the server-side.
4. Why use Node.js?\ 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 Node.js?\ The Event Loop handles asynchronous operations in Node.js,
allowing non-blocking execution by offloading tasks and executing callbacks.
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.
11. What is a module in Node.js?\ A module is a reusable block of code exported using module.exports
and imported using require() .
1
12. What is NPM?\ Node Package Manager, used to manage Node.js 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 package.json?\ A JSON file storing project metadata, dependencies, and scripts.
// math.js
module.exports = { add: (a, b) => a + b };
16. What is EventEmitter?\ A class allowing objects to emit and listen for events.
17. What are streams in Node.js?\ Streams handle data in chunks, improving efficiency for large data.
Types: Readable, Writable, Duplex, Transform.
18. What is buffer in Node.js?\ A temporary memory area for storing binary data, used with streams.
20. Difference between readFile and createReadStream?\ readFile loads entire file in memory;
createReadStream reads data in chunks.
const fs = require('fs');
fs.readFile('test.txt', 'utf8', (err, data) => console.log(data));
2
23. What is middleware?\ Functions that execute during the request-response cycle, widely used in
Express.js.
24. What is cluster module?\ Allows creating multiple Node.js 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.
29. What is process in Node.js?\ An object representing the running Node.js process, giving control and
information.
31. What is Express.js?\ A lightweight Node.js framework for building web servers and APIs.
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 Node.js app?\ Input validation, Helmet.js for headers, rate limiting, and avoiding
injection attacks.
40. What is CORS?\ Cross-Origin Resource Sharing, allows or restricts resource access from different
domains.
42. What is the difference between process.exit() and process.kill()?\ process.exit() terminates the
current process; process.kill() sends a signal to terminate.
43. How to debug Node.js?\ Using console logs, Node Inspector, Chrome DevTools, or VSCode debugger.
44. What is load balancing in Node.js?\ Distributing traffic across multiple processes for better scalability.
47. What is difference between fork and cluster?\ fork() creates child processes; cluster uses
fork() to create multiple server instances.
emitter.setMaxListeners(20);