Node.js Interview Questions

30 Questions

Node.js Basics

Q1

What is Node.js?

Node.js is an open-source JavaScript runtime environment built on Chrome's V8 engine. It allows developers to run JavaScript outside the browser and build fast, scalable server-side applications using event-driven and non-blocking architecture. Example: console.log("Hello Node.js");

Q2

What are the features of Node.js?

Node.js provides asynchronous programming, non-blocking I/O, event-driven architecture, fast execution using the V8 engine, and scalability. It also supports npm packages and cross-platform development, making it popular for backend applications, APIs, and real-time systems. Example: node app.js

Q3

Why is Node.js single-threaded?

Node.js uses a single-threaded event loop to handle multiple requests efficiently without creating multiple threads. This reduces memory usage and improves performance for I/O-heavy tasks like APIs, file handling, and real-time communication applications. Example: setTimeout(() => { console.log("Executed"); }, 1000);

Q4

What is the event-driven architecture in Node.js?

Event-driven architecture means Node.js executes code based on events and listeners. Instead of waiting for tasks to finish, Node.js triggers callbacks when events occur, allowing efficient handling of asynchronous operations and multiple user requests simultaneously. Example: eventEmitter.on("start", () => { console.log("Started"); });

Q5

What is the difference between Node.js and JavaScript?

JavaScript is a programming language mainly used in browsers, while Node.js is a runtime environment that executes JavaScript outside browsers. Node.js provides server-side features like file systems, networking, and process management, which standard JavaScript alone cannot handle. Example: const fs = require("fs");

Q6

What is npm in Node.js?

npm, or Node Package Manager, is a tool used to install, manage, and share JavaScript packages. It provides access to thousands of open-source libraries that help developers speed up application development and manage project dependencies efficiently. Example: npm install express

Q7

What is the package.json file?

package.json is a configuration file that stores project information, dependencies, scripts, version details, and metadata. It helps manage Node.js applications and allows easy installation of required packages across different environments. Example: { "name": "myapp", "version": "1.0.0" }

Q8

What are modules in Node.js?

Modules are reusable blocks of code organized into separate files. Node.js provides built-in, local, and third-party modules to improve maintainability and code organization. Modules help developers split large applications into smaller manageable parts. Example: module.exports = add;

Q9

What is the difference between CommonJS and ES Modules?

CommonJS uses require() and module.exports, while ES Modules use import and export syntax. CommonJS is the default module system in older Node.js versions, whereas ES Modules follow modern JavaScript standards and support static analysis. Example: import fs from "fs";

Q10

What is the purpose of the require() function?

The require() function imports modules, files, or packages into a Node.js application. It helps reuse code from built-in modules, local files, or installed npm packages, improving modularity and maintainability. Example: const os = require("os");

Event Loop & Async Programming

Q11

What is the event loop in Node.js?

The event loop is the mechanism that handles asynchronous operations in Node.js. It continuously checks the call stack and callback queue, executing pending tasks when the stack becomes empty, enabling non-blocking behavior in applications. Example: console.log("Start"); setTimeout(() => console.log("End"), 0);

Q12

What is asynchronous programming in Node.js?

Asynchronous programming allows tasks to execute without blocking the main thread. Node.js handles operations like API requests, database queries, and file reading asynchronously, improving performance and allowing multiple operations to run efficiently. Example: fs.readFile("file.txt", () => { console.log("Done"); });

Q13

What is the difference between synchronous and asynchronous code?

Synchronous code executes line by line and waits for each operation to finish. Asynchronous code allows other tasks to continue while waiting for operations like file reading or API responses, improving speed and responsiveness. Example: console.log("A"); setTimeout(() => console.log("B"), 1000);

Q14

What are callbacks in Node.js?

Callbacks are functions passed as arguments to execute after another function completes its task. They are commonly used in asynchronous operations like file handling and API requests but can become difficult to manage in deeply nested structures. Example: function greet(callback) { callback(); }

Q15

What is callback hell?

Callback hell occurs when multiple nested callbacks make code difficult to read, debug, and maintain. It commonly happens in asynchronous programming and is usually solved using Promises or async/await syntax. Example: doTask(() => { doAnotherTask(() => {}); });

Q16

What are Promises in Node.js?

Promises are objects representing the future completion or failure of asynchronous operations. They improve readability compared to callbacks and support chaining using .then() and .catch() methods for cleaner asynchronous code handling. Example: const promise = new Promise((resolve) => { resolve("Success"); });

Q17

What is async/await in Node.js?

async/await simplifies working with Promises by making asynchronous code appear synchronous. The await keyword pauses execution until a Promise resolves, improving readability and reducing callback nesting in modern Node.js applications. Example: async function getData() { await fetchData(); }

Q18

What is non-blocking I/O?

Non-blocking I/O allows Node.js to continue executing other tasks without waiting for operations like file reading or database queries to finish. This architecture helps Node.js efficiently manage many simultaneous connections and requests. Example: fs.readFile("data.txt", () => { console.log("File loaded"); });

Q19

What is setTimeout() in Node.js?

setTimeout() executes a function after a specified delay in milliseconds. It is commonly used for scheduling delayed operations, timers, and asynchronous task execution inside Node.js applications. Example: setTimeout(() => { console.log("Hello"); }, 2000);

Q20

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

process.nextTick() executes callbacks immediately after the current operation, before the event loop continues. setImmediate() schedules execution during the next event loop cycle. nextTick() has higher priority than setImmediate(). Example: process.nextTick(() => console.log("Next Tick")); setImmediate(() => console.log("Immediate"));

Core Modules & File System

Q21

What is the fs module in Node.js?

The fs module provides file system operations like creating, reading, updating, deleting, and renaming files. It supports both synchronous and asynchronous methods, making it essential for handling server-side file management tasks. Example: const fs = require("fs");

Q22

How do you read files in Node.js?

Files are commonly read using the fs.readFile() method asynchronously or fs.readFileSync() synchronously. The file content is returned through a callback or directly, depending on the chosen method. Example: fs.readFile("test.txt", "utf8", (err, data) => { console.log(data); });

Q23

How do you write files in Node.js?

Files can be written using the fs.writeFile() method asynchronously or fs.writeFileSync() synchronously. These methods create new files or overwrite existing file content with provided data. Example: fs.writeFile("demo.txt", "Hello", () => {});

Q24

What is the path module in Node.js?

The path module helps handle and manipulate file and directory paths. It provides utilities for joining paths, resolving absolute paths, extracting file extensions, and working across different operating systems reliably. Example: const path = require("path");

Q25

What is the os module in Node.js?

The os module provides information about the operating system, including CPU details, memory usage, hostname, platform, and network interfaces. It helps applications interact with system-level information. Example: const os = require("os"); console.log(os.platform());

Q26

What are streams in Node.js?

Streams handle reading and writing data continuously instead of loading everything into memory at once. They improve performance and efficiency when working with large files, video streaming, or real-time data processing. Example: const stream = fs.createReadStream("file.txt");

Q27

What is the difference between readable and writable streams?

Readable streams are used to read data gradually from a source, while writable streams send data to a destination gradually. Both improve memory efficiency and are commonly used for file handling and network operations. Example: fs.createReadStream("input.txt"); fs.createWriteStream("output.txt");

Q28

What is buffering in Node.js?

Buffering temporarily stores binary data in memory before processing or transferring it. Buffers are useful for handling streams, file operations, and network communication where raw binary data management is required. Example: const buffer = Buffer.from("Hello");

Q29

What are events in Node.js?

Events are actions or occurrences detected by Node.js during application execution. Node.js uses an event-driven model where listeners respond to specific events, enabling asynchronous and efficient handling of operations. Example: emitter.emit("login");

Q30

What is the EventEmitter class in Node.js?

EventEmitter is a core Node.js class used to create and handle custom events. It allows applications to register listeners and trigger events dynamically, supporting event-driven programming patterns efficiently. Example: const EventEmitter = require("events"); const emitter = new EventEmitter();