Interview Questions

Get ready for your next interview with our comprehensive question library

Node.js Interview Questions

Filter by Difficulty

1.

What is the V8 engine and how does Node.js use it?

beginner

V8 is Google's open-source JavaScript engine written in C++. It compiles JavaScript directly to native machine code before executing it, making it very fast.

Node.js uses V8 to:

  • Parse and execute JavaScript code
  • Handle memory management and garbage collection
  • Provide JavaScript runtime capabilities on the server
  • Interface with system-level operations through libuv

V8 features include just-in-time (JIT) compilation, hidden class optimization, and efficient garbage collection.

2.

Explain the concept of non-blocking I/O in Node.js.

beginner

Non-blocking I/O means that I/O operations (like reading files, database queries, network requests) don't stop the execution of other code. Instead of waiting for an operation to complete, Node.js continues executing other code and handles the I/O result when it's ready.

// Non-blocking example
const fs = require('fs');

console.log('Start reading file');
fs.readFile('large-file.txt', (err, data) => {
    console.log('File read complete');
});
console.log('This executes immediately');

// Output: Start reading file, This executes immediately, File read complete

This allows Node.js to handle thousands of concurrent connections efficiently.

3.

What are callbacks and what is callback hell?

beginner

Callbacks are functions passed as arguments to other functions, executed when an asynchronous operation completes. Callback hell occurs when multiple nested callbacks make code difficult to read and maintain.

// Callback hell example
getData((err, data) => {
    if (err) throw err;
    processData(data, (err, processed) => {
        if (err) throw err;
        saveData(processed, (err, result) => {
            if (err) throw err;
            console.log('Data saved successfully');
        });
    });
});

Solutions include Promises, async/await, and modularizing code into smaller functions.

4.

Explain the difference between require() and import statements.

beginner
  • require(): CommonJS module system, synchronous loading, runtime evaluation
  • import: ES6 module system, static analysis, compile-time evaluation
// CommonJS (require)
const fs = require('fs');
const { readFile } = require('fs');

// ES6 Modules (import)
import fs from 'fs';
import { readFile } from 'fs';

// Dynamic import
const module = await import('./dynamic-module.js');

Key differences:

  • require() can be called conditionally; import must be at top level
  • import allows tree-shaking for smaller bundles
  • require() is synchronous; import can be asynchronous
5.

What is the module.exports vs exports difference?

beginner
  • module.exports: The actual object that gets returned when module is required
  • exports: A reference to module.exports, convenient for adding properties
// Method 1: Using exports (shorthand)
exports.add = (a, b) => a + b;
exports.subtract = (a, b) => a - b;

// Method 2: Using module.exports
module.exports = {
    add: (a, b) => a + b,
    subtract: (a, b) => a - b
};

// Method 3: Exporting a single function
module.exports = (a, b) => a + b;

// Wrong: This breaks the reference
exports = { add: (a, b) => a + b }; // Won't work
6.

What is npm and what are npm scripts?

beginner

npm (Node Package Manager) is the default package manager for Node.js, used to install, manage, and share JavaScript packages.

npm scripts are commands defined in package.json that can be executed using npm run <script-name>:

{
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js",
    "test": "jest",
    "build": "webpack --mode production",
    "lint": "eslint .",
    "prestart": "npm run build"
  }
}

Benefits:

  • Standardized project commands
  • Environment-specific configurations
  • Lifecycle hooks (pre/post scripts)
  • Cross-platform compatibility
7.

Explain package.json and package-lock.json.

beginner

package.json: Contains project metadata, dependencies, and scripts

{
  "name": "my-app",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.18.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

package-lock.json: Locks exact versions of dependencies and their sub-dependencies for reproducible builds

  • Ensures consistent installs across environments
  • Contains integrity hashes for security
  • Maps exact dependency tree structure
  • Should be committed to version control
8.

How do you read and write files in Node.js?

beginner

Node.js provides both synchronous and asynchronous methods for file operations:

const fs = require('fs');

// Asynchronous (recommended)
fs.readFile('input.txt', 'utf8', (err, data) => {
    if (err) throw err;
    console.log('File content:', data);
});

fs.writeFile('output.txt', 'Hello World', (err) => {
    if (err) throw err;
    console.log('File written successfully');
});

// Synchronous (blocking)
try {
    const data = fs.readFileSync('input.txt', 'utf8');
    fs.writeFileSync('output.txt', data.toUpperCase());
} catch (err) {
    console.error('Error:', err);
}

// Promise-based
const fsPromises = require('fs').promises;
const data = await fsPromises.readFile('input.txt', 'utf8');
9.

How do you create a basic HTTP server in Node.js?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
10.

What is the difference between http and https modules?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
11.

How do you make HTTP requests in Node.js?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What is Express.js and why is it popular?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
13.

How do you handle routing in Express.js?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
14.

What is the difference between app.use() and app.get()?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How do you handle form data and JSON in Express?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
16.

How do you handle environment configuration in Node.js?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
17.

Explain the Node.js Event Loop and how it works.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

Explain Promises in Node.js with examples.

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

What is async/await and how does it improve asynchronous code?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 57 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime