Interview-Bytes
Expertly curated technical interview questions,
simplified into manageable lessons.
Filter by Topic
Showing 1–11 of 11 questions
What do you mean by "Node.js is a JavaScript runtime"?
Historically, JavaScript only ran in browsers and was limited to DOM manipulation and event handling.
Node.js allows JavaScript to run outside the browser using chrome’s V8 engine
It provides an environment where JS can interact with the system, making it usable for backend development.
It allows JavaScript to handle backend tasks that were traditionally handled by languages like Java or Python.
❓ What is the Node.js event loop?
Node.js uses an event loop to manage asynchronous tasks while staying single-threaded.
It handles tasks like file I/O & API calls outside the main thread, running callbacks only when the main thread is free.
Example -
Sync tasks(start,end) run first => Async task(Timeout) placed in Queue => After 1 sec event loop checks & executes callback.
console.log("start");
setTimeout(() => console.log("Timeout"), 1000);
console.log("end");
📦 What is package.json in Node.js?
File that manager our project, It is automatically created when we do npm init. It has -
Project Metadata: Contains name, version, description.
Dependency Management: Lists required packages.
Script Commands: Defines npm scripts for automation (e.g., start, test).
Version Control: Ensures consistent package versions across environments.
Configuration: Stores settings for tools & libraries.
🚀 What is npm in Node.js?
A package manager/ tool to manage dependencies, scripts and libraries in Node.js project. It allows to include 3rd party code. It is comparable to pip in python and maven in java.
❓What is a Module System in Node.js?
Module is reusable code. A module system is used to organize code in seperate functionalities.
Types of Module System -
1️⃣ CommonJs- Default Module system in nodejs. It uses require() to import and module.exports. Synchronously loaded.
2️⃣ Ecmascript Modules(ESM)- New standard introduced in ES6. It uses import and export syntax and is asynchronously loaded.
Note - ESM is now supported in nodejs with .mjs extension or enabling in package.json
🔔 What is EventEmitter in Node.js?
It is part of the core events module, enabling objects to communicate via events (async)
It is used in many core modules like (http, fs etc).
It can be used for event-driven architecture!
const EventEmitter = require('events');
const eventEmitter = new EventEmitter();
// Register an event listener
eventEmitter.on('greet', () => {
console.log('Hello, world!');
});
// Emit the event
eventEmitter.emit('greet');
📂 How Node.js handles file operations?
It uses the fs module for working with the file system.
Use case - When a user requests a file (e.g., an image or document), Node.js reads the file asynchronously, allowing the server to handle other requests.
Asynchronous I/O prevents blocking other requests!
There is another method readFileSync which reads file synchronously.
const fs = require('fs');
app.get('/file', (req, res) => {
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) res.status(500).send('Error reading file');
res.send(data);
});
});
💡 What are Streams in Node.js?
Streams handle I/O efficiently by processing data in chunks instead of loading it all at once. Ideal for large files or real-time data. Types of Stream -
1. Readable: For reading data
2. Writable: For writing data
3. Duplex: Both read & write
4. Transform: Modify data while reading/writing
It is perfect for video streaming, file handling, or large datasets! Below is example of using Streams for File Handling.
const fs = require('fs');
const readStream = fs.createReadStream('largefile.txt');
readStream.on('data', (chunk) => {
console.log(`Received ${chunk.length} bytes of data.`);
});
readStream.on('end', () => {
console.log('Finished reading the file.');
});
💡 What are Callbacks in Node.js?
Callbacks are functions passed as argument to another function and gets executed after the main func has completed its task. In node.js they are crucial for non-blocking behaviour, callbacks are passed to handle async tasks.
How it works ? The async task starts → Node.js keeps working → When done, the event loop triggers the callback to handle the result.
fs.readFile('file.txt', 'utf8', (err, data) => {
if (err) console.error(err);
else console.log(data);
});
💡What are process.nextTick() & setImmediate() in Node.js?
1️⃣ process.nextTick() - Executes before the next event loop. Great for immediate logic after a task, like error handling or cleanup.
2️⃣ setImmediate() - Executes after I/O events in the event loop. Useful for deferred tasks that shouldn’t block the current cycle.
💡 What is the Buffer class in Node.js?
It is a global object which handles raw binary data in memory (Space is allocated in RAM) allowing us to manipulate raw data streams efficiently
It is great for file I/O and Network communications
Example - Reading a binary file using buffer. fs.readFile reads binary data from file & saves in buffer. Buffer.toString(‘base64’) is used to convert raw binary data to base64
const fs = require('fs');
// Read a binary file (like an image) as a buffer
fs.readFile('image.png', (err, data) => {
if (err) throw err;
// `data` is a Buffer containing the binary content of the file
console.log('Original Buffer:', data);
// Convert the Buffer to a string in base64 encoding
// Useful when we want to send it over a network
const base64Data = data.toString('base64');
console.log('Base64 Encoded Data:', base64Data);
});