1. Core Concepts & Fundamentals (40 Questions)
Q1: What is Node.js?
Answer: Node.js is an open-source, cross-platform JavaScript runtime environment built on Chrome's V8 JavaScript engine. It allows developers to run JavaScript code outside of a web browser, enabling server-side scripting.
Q2: How does Node.js differ from traditional server-side technologies?
Answer:
Non-blocking I/O: Uses event-driven, asynchronous architecture
Single-threaded: Uses event loop for concurrency
JavaScript everywhere: Same language for frontend and backend
Fast execution: Built on V8 engine with Just-In-Time compilation
Q3: Explain the event-driven architecture of Node.js
Answer: Node.js uses an event-driven architecture where actions (events) trigger callbacks. The Event Loop continuously checks for events and executes their associated callbacks when they occur.
Q4: What is the Event Loop?
Answer: The Event Loop is Node.js's mechanism for handling asynchronous operations. It continuously checks the call stack and callback queue, pushing callbacks to the stack when it's empty.
Q5: Explain Node.js's single-threaded nature
Answer: Node.js runs on a single main thread but uses worker threads for I/O operations. This allows it to handle thousands of concurrent connections efficiently without creating threads for each connection.
Q6: What are the key features of Node.js?
Answer:
Asynchronous and Event Driven
Very Fast (V8 Engine)
Single Threaded but Highly Scalable
No Buffering
MIT License
Large Ecosystem (NPM)
Q7: What is the V8 JavaScript Engine?
Answer: V8 is Google's open-source JavaScript engine written in C++. It compiles JavaScript directly to native machine code before executing it, making it extremely fast.
Q8: What are the different types of API functions in Node.js?
Answer:
Blocking (Synchronous) Functions: Execute immediately, block further execution
Non-blocking (Asynchronous) Functions: Use callbacks, don't block execution
Promise-based Functions: Return promises, can use async/await
Q9: What is REPL in Node.js?
Answer: REPL stands for Read-Eval-Print-Loop. It's an interactive Node.js shell where you can execute JavaScript code directly.
Q10: How do you start a REPL session?
Answer: Simply type node in the terminal without any filename:
nodeQ11: What is process.argv?
Answer: process.argv is an array containing command-line arguments passed when starting the Node.js process.
Q12: What is process.env?
Answer: process.env is an object containing user environment information. Commonly used to access environment variables.
Q13: Explain global objects in Node.js
Answer: Global objects are available in all modules without requiring import. Examples: global, process, console, Buffer, setTimeout, setInterval.
Q14: What is the purpose of __filename and __dirname?
Answer:
__filename: Absolute path of the current module file__dirname: Directory name of the current module
Q15: How does Node.js handle child processes?
Answer: Node.js provides child_process module to create child processes. Methods include:
spawn(): Launches new processexec(): Runs command in shellfork(): Special case of spawn() for Node.js processes
Q16: What is the difference between development and production modes?
Answer:
Development: Detailed error messages, hot reloading, verbose logging
Production: Optimized performance, minimal logging, error handling
Q17: How do you set environment to production?
Answer:
export NODE_ENV=production # Linux/Mac set NODE_ENV=production # Windows # OR in package.json "scripts": { "start": "NODE_ENV=production node app.js" }
Q18: What is a callback in Node.js?
Answer: A callback is a function passed as an argument to another function, to be executed after the completion of an operation.
Q19: What is callback hell and how to avoid it?
Answer: Callback hell is nested callbacks making code unreadable. Avoid using:
Promises
Async/Await
Modularization
Control flow libraries
Q20: What is N-API?
Answer: N-API is an API for building native addons in Node.js. It's independent of the underlying JavaScript runtime.
Q21: What is libuv?
Answer: libuv is a multi-platform C library that provides support for asynchronous I/O operations. Node.js uses it for the event loop and thread pool.
Q22: How does Node.js handle memory management?
Answer: Node.js uses V8's garbage collector which employs:
Generational collection
Mark-and-sweep algorithm
Incremental collection
Memory limits (default ~1.7GB)
Q23: What is the difference between setImmediate() and process.nextTick()?
Answer:
process.nextTick(): Executes before the next iteration of the event loopsetImmediate(): Executes in the next iteration of the event loop
Q24: What is the purpose of util.promisify()?
Answer: Converts callback-based functions to promise-based functions:
const util = require('util'); const fs = require('fs'); const readFile = util.promisify(fs.readFile);
Q25: Explain Node.js's module caching
Answer: Node.js caches modules after the first require. Subsequent requires return the cached version.
Q26: What is the difference between require() and import()?
Answer:
require(): CommonJS, synchronous, runtime importimport(): ES6, asynchronous, static analysis possible
Q27: How does Node.js handle CPU-intensive tasks?
Answer: Node.js uses:
Worker Threads
Child Processes
Clustering
External services/microservices
Q28: What is the purpose of os module?
Answer: The os module provides operating system-related utility methods for memory, CPUs, network interfaces, etc.
Q29: What is the difference between process.exit() and process.kill()?
Answer:
process.exit(): Ends process with exit codeprocess.kill(): Sends signal to process (like SIGTERM)
Q30: What is the purpose of util.inherits()?
Answer: Inherits methods from one constructor to another (legacy, use ES6 classes instead).
Q31: How can you check Node.js version?
Answer:
node --version # OR in code console.log(process.version);
Q32: What is the purpose of vm module?
Answer: The vm module enables compiling and running code within V8 Virtual Machine contexts.
Q33: What is the difference between process.stdout and console.log?
Answer: console.log internally uses process.stdout.write but adds newline and formatting.
Q34: What are Node.js streams?
Answer: Streams are collections of data that might not be available all at once. Types: Readable, Writable, Duplex, Transform.
Q35: What is the purpose of Buffer class?
Answer: Buffer handles binary data directly, useful for file operations, network communications, etc.
Q36: How does Node.js handle circular dependencies?
Answer: Node.js can handle circular dependencies but returns incomplete modules. Best to avoid by restructuring code.
Q37: What is the purpose of URL module?
Answer: Provides utilities for URL resolution and parsing.
Q38: What is the difference between exports and module.exports?
Answer: exports is a reference to module.exports. Assigning to exports breaks the reference.
Q39: How do you debug Node.js applications?
Answer:
console.log()statementsNode.js debugger (
node inspect)Chrome DevTools (
--inspectflag)VS Code debugger
Q40: What is the purpose of assert module?
Answer: Provides set of assertion functions for testing invariants.
2. Modules & NPM (30 Questions)
Q41: What is a module in Node.js?
Answer: A module is a reusable block of code whose existence does not accidentally impact other code. Each file is treated as a separate module.
Q42: How do you create your own module?
Answer:
// myModule.js module.exports = { myFunction: function() { return "Hello from module"; } }; // app.js const myModule = require('./myModule'); console.log(myModule.myFunction());
Q43: What is package.json?
Answer: package.json is the manifest file for Node.js projects containing metadata, dependencies, scripts, and configuration.
Q44: What is the difference between dependencies and devDependencies?
Answer:
dependencies: Required for production
devDependencies: Required only for development (testing, building, etc.)
Q45: What is the purpose of package-lock.json?
Answer: Locks dependency versions to ensure consistent installs across environments.
Q46: What is semantic versioning?
Answer: Version format: MAJOR.MINOR.PATCH
MAJOR: Breaking changes
MINOR: New features, backward compatible
PATCH: Bug fixes, backward compatible
Q47: What do ^ and ~ mean in package.json versions?
Answer:
^1.2.3: Accepts minor and patch updates (1.x.x)~1.2.3: Accepts only patch updates (1.2.x)1.2.3: Exact version
Q48: How do you update npm packages?
Answer:
npm update # Update all packages npm update <package> # Update specific package npm outdated # Check for outdated packages
Q49: What is npx?
Answer: npx is a package runner that executes packages without installing them globally.
Q50: What is the difference between npm install and npm ci?
Answer:
npm install: Installs packages, updates lockfilenpm ci: Clean install, uses lockfile exactly, deletes node_modules first
Q51: How do you create global packages?
Answer:
npm install -g <package-name> # OR in package.json "bin": { "my-cli": "./cli.js" }
Q52: What is npm scripts?
Answer: Commands defined in package.json that can be run with npm run <script-name>.
Q53: What is the purpose of .npmrc file?
Answer: Configuration file for npm containing registry settings, authentication, etc.
Q54: How do you publish a package to npm?
Answer:
npm login npm publish # Private packages require paid account
Q55: What is npm audit?
Answer: Scans project dependencies for security vulnerabilities.
Q56: How do you fix npm audit vulnerabilities?
Answer:
npm audit fix # Fix automatically npm audit fix --force # Fix breaking changes
Q57: What is the purpose of engines field in package.json?
Answer: Specifies Node.js and npm versions required:
"engines": { "node": ">=14.0.0", "npm": ">=6.0.0" }