If you are preparing for your first Node.js interview, you are probably wondering — "What will they actually ask me?" The good news is that most interviews follow a pattern. Master these 15 questions and you will walk in with confidence.
Let's get straight to it.
1. What is Node.js?
Node.js is an open-source JavaScript runtime built on Chrome's V8 engine. It lets you run JavaScript on the server side — outside the browser.
Before Node.js, JavaScript only worked in browsers. Node.js changed that by making JavaScript a full-stack language.
Key features:
Event-driven and non-blocking
Fast, thanks to the V8 engine
Great for building APIs, real-time apps, and microservices
2. Why is Node.js single-threaded? Is that a problem?
Node.js runs on a single main thread. But this is NOT a weakness — it is a design choice.
Instead of creating a new thread for every request (which wastes memory), Node.js uses an event loop to handle thousands of requests asynchronously on one thread.
When it IS a problem: CPU-heavy tasks like video encoding or complex calculations can block the thread. For those cases, Node.js has Worker Threads.
Simple rule to remember:
I/O tasks (reading files, API calls, database queries) → Node.js handles perfectly
CPU-heavy tasks → use Worker Threads or a separate service
3. What is the Event Loop?
The Event Loop is the core of Node.js. It is what makes non-blocking, asynchronous code possible.
How it works in simple terms:
You make an async request (like reading a file)
Node.js sends it to the system and moves on
When the file is ready, the callback is queued
The Event Loop picks it up and runs it
console.log('Start');
setTimeout(() => {
console.log('Inside setTimeout');
}, 0);
console.log('End');
// Output:
// Start
// End
// Inside setTimeout
Even though the timeout is 0ms, it runs last — because it goes through the event loop.
4. What is npm?
npm stands for Node Package Manager. It comes bundled with Node.js and is the world's largest software registry.
With npm you can:
Install libraries:
npm install expressManage your project dependencies
Run scripts like
npm startornpm testShare your own packages with the world
5. What is package.json?
package.json is the configuration file of every Node.js project. Think of it as the identity card of your project.
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"dependencies": {
"express": "^4.18.0"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
dependencies — packages needed in production. devDependencies — packages only needed during development.
6. What is the difference between require() and import?
Both are used to include external modules, but they come from different systems.
require() is from the CommonJS system — the traditional Node.js way. It is synchronous and widely used in existing Node.js projects.
import is from the ES Modules system — the modern JavaScript standard. It is used in frontend frameworks like React and is becoming more common in Node.js too.
// CommonJS — require()
const express = require('express');
// ES Module — import
import express from 'express';
For most beginner Node.js projects, you will see require() everywhere. Just know both exist.
7. What is module.exports?
When you create a file in Node.js, nothing inside it is accessible outside by default. You use module.exports to expose what you want to share.
// math.js
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
module.exports = { add, subtract };// app.js
const math = require('./math');
console.log(math.add(5, 3)); // 8
console.log(math.subtract(5, 3)); // 2
8. What is a Callback?
A callback is a function that you pass into another function, to be called later — usually after an async task finishes.
const fs = require('fs');
fs.readFile('hello.txt', 'utf8', function(err, data) {
if (err) {
console.log('Error reading file:', err);
return;
}
console.log('File content:', data);
});
console.log('This runs first!');
Notice that "This runs first!" prints before the file content — because readFile is asynchronous.
Important convention: The first argument of a callback is always the error object. If there is no error, it will be null.
9. What is Callback Hell and how do you avoid it?
When you nest callbacks inside callbacks inside callbacks, you get what developers call Callback Hell — code that is almost impossible to read or maintain.
// Callback Hell — hard to read
getUser(id, function(err, user) {
getPosts(user, function(err, posts) {
getComments(posts[0], function(err, comments) {
saveLog(comments, function(err, result) {
// This keeps going...
});
});
});
});
The fix — use async/await:
// Clean and readable
async function loadData(id) {
try {
const user = await getUser(id);
const posts = await getPosts(user);
const comments = await getComments(posts[0]);
const result = await saveLog(comments);
return result;
} catch (err) {
console.log('Something went wrong:', err);
}
}
Same logic, but now it reads like normal top-to-bottom code.
10. What is a Promise?
A Promise is an object that represents the eventual result of an async operation. It can be in one of three states:
Pending — still waiting
Fulfilled — completed successfully
Rejected — failed with an error
function fetchUser(id) {
return new Promise((resolve, reject) => {
if (id <= 0) {
reject(new Error('Invalid ID'));
} else {
resolve({ id: id, name: 'Rahul' });
}
});
}
// Using the promise
fetchUser(1)
.then(user => console.log('User:', user.name))
.catch(err => console.log('Error:', err.message));
Promises are the foundation of async/await — under the hood, async/await is just cleaner syntax for promises.
11. What is async/await?
async/await is the modern way to write asynchronous code in Node.js. It makes async code look and behave like synchronous code — clean and easy to follow.
Rules:
Add
asyncbefore a function to make it return a PromiseUse
awaitinside an async function to wait for a Promise to resolveAlways wrap in
try/catchto handle errors
async function getUserData(userId) {
try {
const user = await fetchUser(userId);
const posts = await fetchPosts(user.id);
console.log(`${user.name} has ${posts.length} posts`);
} catch (err) {
console.log('Error:', err.message);
}
}
getUserData(1);
12. What are Streams in Node.js?
Streams let you process data piece by piece instead of loading everything into memory at once.
Imagine you want to read a 2GB video file. Loading the whole thing into memory would crash your server. With streams, you read and send it in small chunks.
const fs = require('fs');
const http = require('http');
const server = http.createServer((req, res) => {
// Without stream — loads entire file into memory (bad for large files)
// fs.readFile('video.mp4', (err, data) => res.end(data));
// With stream — sends chunks as they are read (memory efficient)
const stream = fs.createReadStream('video.mp4');
stream.pipe(res);
});
server.listen(3000);
4 types of streams:
Readable — for reading data
Writable — for writing data
Duplex — for both reading and writing
Transform — for modifying data while reading or writing
13. What is Middleware in Express.js?
Middleware are functions that run between receiving a request and sending a response. Every request in Express passes through a chain of middleware functions.
const express = require('express');
const app = express();
// Middleware 1 — runs for every request
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next(); // Must call next() to pass to the next middleware
});
// Middleware 2 — parse JSON body
app.use(express.json());
// Route handler — final destination
app.get('/users', (req, res) => {
res.json({ users: [] });
});
app.listen(3000);
If you forget to call next(), your request will hang forever — nothing will be sent back to the client.
14. How do you handle errors in Node.js?
Error handling is something many beginners ignore — but it is one of the first things interviewers check.
// Synchronous error — use try/catch
try {
const data = JSON.parse('invalid json{{{');
} catch (err) {
console.log('Parse error:', err.message);
}
// Async error with async/await
async function getData() {
try {
const result = await fetchFromDatabase();
return result;
} catch (err) {
console.log('Database error:', err.message);
throw err; // re-throw if needed
}
}
// Express error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).json({ message: 'Something went wrong!' });
});
// Catch unhandled promise rejections globally
process.on('unhandledRejection', (err) => {
console.error('Unhandled Rejection:', err);
process.exit(1);
});
15. What is the Event Emitter?
EventEmitter is a built-in Node.js class that lets you create and listen to custom events. It is the backbone of almost every Node.js module — HTTP, Streams, and more are all built on it.
const EventEmitter = require('events');
// Create an emitter
const shop = new EventEmitter();
// Listen for the 'order' event
shop.on('order', (item) => {
console.log(`New order received: ${item}`);
});
// Listen once — automatically removes after first call
shop.once('open', () => {
console.log('Shop is open for the first time!');
});
// Trigger the events
shop.emit('order', 'Pizza'); // New order received: Pizza
shop.emit('order', 'Burger'); // New order received: Burger
shop.emit('open'); // Shop is open for the first time!
shop.emit('open'); // Nothing — 'once' already fired
Quick Revision — 15 Questions at a Glance
1. Node.js — JavaScript runtime on the server, built on V8 engine
2. Single-threaded — Uses event loop for concurrency, not multiple threads
3. Event Loop — Handles async callbacks without blocking the main thread
4. npm — Node Package Manager, installs and manages libraries
5. package.json — Project config file, lists dependencies and scripts
6. require vs import — CommonJS (old) vs ES Modules (modern)
7. module.exports — Used to expose functions/objects from a file
8. Callback — Function passed to another function, called after async task
9. Callback Hell — Nested callbacks, fixed with async/await
10. Promise — Object representing future result: pending, fulfilled, or rejected
11. async/await — Clean syntax for writing async code
12. Streams — Process large data in chunks, memory efficient
13. Middleware — Functions that run between request and response in Express
14. Error Handling — try/catch, error middleware, unhandledRejection
15. Event Emitter — Built-in pub/sub system, backbone of Node.js core modules
Before You Go — 3 Interview Tips
Tip 1 — Understand, don't memorize. If you understand WHY the event loop works the way it does, you can answer any variation of that question. Rote memorization fails when the interviewer goes off-script.
Tip 2 — Be ready to write code. Interviewers will ask you to write a simple async function, create an Express route, or fix a broken callback. Practice writing code — not just reading it.
Tip 3 — Talk through your thinking. If you are unsure, say "I think it works like this because..." Interviewers value how you think, not just whether you get the right answer.

