Node.js Interview Questions: Beginner to Expert
Practice 60 popular Node.js interview questions covering fundamentals, asynchronous programming, Express, databases, security, performance, concurrency, scalability, and production architecture.
What is Node.js?
Node.js is an open-source, cross-platform JavaScript runtime environment that allows JavaScript to run outside the browser. It is built on Google's V8 JavaScript engine and is commonly used for backend services, APIs, command-line tools, real-time applications, and automation. Node.js uses an event-driven architecture and non-blocking I/O, making it well suited for applications that handle many concurrent I/O operations.
Node.js is a runtime, not a programming language.
It uses the V8 JavaScript engine.
It is commonly used for server-side JavaScript.
Its event-driven model is suitable for I/O-heavy applications.
What is Node.js?
Is Node.js a programming language?
No. Node.js is not a programming language. JavaScript is the programming language, while Node.js is a runtime environment that provides APIs and infrastructure for executing JavaScript outside a browser. Node.js provides capabilities such as file-system access, networking, streams, processes, and HTTP servers.
JavaScript is the language.
Node.js is the runtime.
Node.js adds server-side and system-level APIs to JavaScript.
Which statement correctly describes Node.js?
Why is Node.js popular for backend development?
Node.js is popular because it allows developers to use JavaScript across the frontend and backend, has a large npm ecosystem, provides efficient non-blocking I/O, and works well for APIs, real-time applications, microservices, and other I/O-heavy systems. Its asynchronous programming model can efficiently handle many concurrent requests without creating one operating-system thread per request.
JavaScript can be used across the stack.
npm provides a large package ecosystem.
Non-blocking I/O supports high concurrency.
Node.js is particularly strong for I/O-heavy workloads.
Which characteristic makes Node.js particularly useful for I/O-heavy backend applications?
What JavaScript engine does Node.js use?
Node.js uses the V8 JavaScript engine, originally developed by Google for Chrome. V8 parses and executes JavaScript and uses techniques such as just-in-time compilation to optimize JavaScript execution. Node.js adds its own runtime APIs around the JavaScript engine.
Node.js uses V8.
V8 executes JavaScript.
Node.js adds runtime APIs around V8.
Which JavaScript engine powers Node.js?
What is npm?
npm originally stood for Node Package Manager and is the standard package manager commonly used with Node.js. It is used to install, update, remove, and manage project dependencies and to run scripts defined in package.json. npm also provides access to a large public package registry.
npm manages dependencies.
npm can execute package.json scripts.
npm provides access to a large package registry.
What is npm primarily used for?
What is package.json?
package.json is the manifest file for a Node.js project. It commonly contains the project name, version, scripts, dependencies, development dependencies, module configuration, metadata, and other project settings. It allows npm and other tooling to understand how the project should be installed and executed.
package.json describes a Node.js project.
Dependencies and scripts are commonly defined there.
It is a central configuration file for npm projects.
What is the main purpose of package.json?
What is package-lock.json?
package-lock.json records the resolved dependency tree and exact package versions installed for a project. It helps ensure reproducible installations across machines and environments. It can contain information about transitive dependencies, package versions, integrity information, and resolved package locations.
package.json declares dependencies.
package-lock.json records resolved dependency versions.
Lock files improve reproducibility.
What is the primary purpose of package-lock.json?
What is the difference between dependencies and devDependencies?
dependencies are packages required by the application at runtime. devDependencies are generally packages needed during development, testing, linting, formatting, or building but not necessarily required to run the production application. For example, Express may be a dependency, while a testing framework or ESLint may be a devDependency.
dependencies are runtime dependencies.
devDependencies are generally development/build/test dependencies.
Correct classification helps production installations remain lean.
Where would a package required by the production application normally be placed?
What is CommonJS?
CommonJS is a module system historically used extensively in Node.js. It uses require() to import modules and module.exports or exports to expose values. CommonJS modules are typically loaded synchronously and have their own module scope.
CommonJS is a Node.js module system.
require() imports modules.
module.exports exports values.
Which syntax is associated with CommonJS modules?
What are ES Modules?
ES Modules, commonly called ESM, are the standardized JavaScript module system using import and export syntax. Modern Node.js supports ESM alongside CommonJS. ESM can be enabled through configuration such as package.json type: module or appropriate file extensions such as .mjs.
ESM uses import/export.
Node.js supports both ESM and CommonJS.
Module-system configuration should be consistent within a project.
Which syntax is used by ES Modules?
What is the difference between require() and import?
require() is the CommonJS module-loading mechanism, while import is the ES Module syntax. They differ in module semantics, resolution behavior, loading characteristics, and configuration. Modern Node.js supports interoperability between the systems, but mixing them without understanding the module configuration can lead to compatibility issues.
require() belongs to CommonJS.
import belongs to ESM.
Avoid inconsistent module configuration.
Which statement is correct?
What is process.env?
process.env is an object exposed by Node.js that provides access to environment variables available to the running process. It is commonly used for configuration such as database URLs, ports, feature flags, and secrets. Sensitive values should be supplied securely rather than committed to source control.
process.env contains environment variables.
Use environment variables for runtime configuration.
Do not commit secrets into source code.
What is process.env commonly used for?
What is process.argv?
process.argv is an array containing the command-line arguments passed to a Node.js process. It is commonly used by CLI applications to read positional arguments and command-line flags. The first entries identify the Node executable and the executed script, followed by user-provided arguments.
process.argv is an array.
It contains command-line arguments.
It is useful for CLI applications.
What does process.argv provide?
What is a Buffer?
A Buffer is a Node.js representation of a fixed-length sequence of bytes. Buffers are useful when working with binary data such as files, network packets, encoded data, and streams. Unlike normal JavaScript strings, Buffers directly represent raw byte data.
Buffers represent bytes.
They are important for binary data.
Streams frequently work with Buffer data.
What does a Node.js Buffer primarily represent?
What are streams?
Streams provide an abstraction for processing data incrementally instead of loading an entire dataset into memory at once. Node.js provides readable, writable, duplex, and transform streams. Streams are useful for files, HTTP requests and responses, compression, encryption, and other continuous data processing tasks.
Streams process data incrementally.
Node.js has readable, writable, duplex, and transform streams.
Streams are useful for large or continuous data.
What is a major benefit of streams?
What is the event loop?
The event loop is a core part of Node.js's asynchronous execution model. JavaScript code primarily runs on the main thread, while asynchronous operations can be handled by the operating system or libuv. When operations complete, their callbacks or promise continuations are scheduled so JavaScript can process them without blocking on the I/O operation itself.
The event loop enables asynchronous execution.
JavaScript execution primarily occurs on the main thread.
Blocking the event loop can reduce application responsiveness.
What is the primary role of the Node.js event loop?
Is Node.js single-threaded?
Node.js is often described as single-threaded because JavaScript execution for a Node.js process normally occurs on a main thread. However, Node.js is not limited to one thread for all work. libuv can use a worker pool for certain operations, Worker Threads provide additional JavaScript threads, and child processes can run work independently. Therefore, saying Node.js is simply single-threaded is incomplete.
The JavaScript execution model is primarily single-threaded.
Node.js can use worker threads and child processes.
libuv also provides a worker pool for certain operations.
Which statement best describes Node.js threading?
How does Node.js handle concurrency?
Node.js handles concurrency primarily through asynchronous, non-blocking I/O and the event loop. When an operation such as network I/O is started, Node.js does not need to synchronously wait for its completion. Once the operation completes, the corresponding callback or promise continuation can be processed. Additional concurrency mechanisms such as Worker Threads and child processes are available for CPU-intensive workloads.
Non-blocking I/O is central to Node.js concurrency.
The event loop coordinates asynchronous work.
CPU-heavy work may require Worker Threads or processes.
How does Node.js primarily achieve high I/O concurrency?
What is libuv?
libuv is a cross-platform library used by Node.js to provide asynchronous I/O capabilities and event-loop infrastructure. It abstracts operating-system differences and provides mechanisms for networking, timers, asynchronous operations, and a worker pool used by certain APIs.
libuv is a core component of Node.js.
It provides cross-platform asynchronous infrastructure.
It includes a worker pool for certain operations.
What is libuv primarily responsible for in Node.js?
What is the difference between synchronous and asynchronous APIs?
Synchronous APIs block the current JavaScript execution flow until the operation completes. Asynchronous APIs allow execution to continue while the operation is pending and later provide the result through a callback, promise, event, or other mechanism. In servers, asynchronous APIs are generally preferred for operations that may take significant time because synchronous operations can block the event loop.
Synchronous operations can block execution.
Asynchronous APIs allow other work to continue.
Avoid unnecessary synchronous operations in request-handling paths.
Why can synchronous file operations be problematic in a Node.js server?
What is a Promise?
A Promise is a JavaScript object representing the eventual completion or failure of an asynchronous operation. It can be pending, fulfilled, or rejected. Promises can be composed with methods such as then(), catch(), and finally(), and can be consumed conveniently using async/await.
Promises represent asynchronous results.
They can be fulfilled or rejected.
async/await is built on Promise-based asynchronous programming.
Which states can a Promise have?
What is async/await?
async/await is syntax for working with Promises using a more synchronous-looking style. An async function always returns a Promise. The await keyword pauses execution within that async function until the awaited Promise settles, without blocking the Node.js event loop itself. Errors can commonly be handled using try/catch.
async functions return Promises.
await consumes Promise results.
try/catch can handle rejected awaited Promises.
What does an async function return?
What is callback hell?
Callback hell describes deeply nested callback-based asynchronous code that becomes difficult to read, maintain, test, and handle errors in. It often occurs when multiple dependent asynchronous operations are nested inside each other. Promises, async/await, modular functions, and better control-flow design can reduce this problem.
Callback hell is mainly a code-structure problem.
Promises can flatten asynchronous control flow.
async/await can make dependent operations easier to read.
Which approach commonly helps reduce callback hell?
What is process.nextTick()?
process.nextTick() schedules a callback to execute after the current operation completes but before the event loop proceeds to later phases. It is useful for deferring work until the current call stack has completed. However, recursively scheduling nextTick callbacks can starve the event loop and prevent I/O from being processed.
nextTick defers execution until the current operation finishes.
It has very high scheduling priority.
Excessive recursive use can starve I/O.
What is an important characteristic of process.nextTick()?
What is setImmediate()?
setImmediate() schedules a callback to run during the event loop's check phase. It is commonly useful when you want to defer execution until after the current poll phase. Its ordering relative to timers can depend on context, so it should not be treated as simply equivalent to setTimeout(fn, 0).
setImmediate runs during the check phase.
It differs from setTimeout(fn, 0).
Execution ordering can depend on the context in which they are scheduled.
During which event-loop phase are setImmediate callbacks executed?
What is Express middleware?
Express middleware is a function that has access to the request object, response object, and next function. Middleware can inspect or modify requests and responses, perform authentication, logging, validation, parsing, and other cross-cutting tasks. Middleware can either end the response or call next() to continue processing.
Middleware participates in request processing.
It can modify req and res.
next() continues the middleware chain.
What does next() generally do in Express middleware?
What is the difference between req.params, req.query, and req.body?
req.params contains route parameters such as /users/:id. req.query contains query-string parameters such as /users?page=2. req.body contains data sent in the request body, typically for POST, PUT, or PATCH requests after appropriate body-parsing middleware has processed it.
req.params contains route parameters.
req.query contains query-string parameters.
req.body contains request-body data.
For the route /users/123?active=true, where would the value 123 normally be found?
How does Express error-handling middleware work?
Express error-handling middleware uses four parameters: err, req, res, and next. It is used to centralize error handling and convert application errors into appropriate HTTP responses. A typical application defines error handling after its routes and other middleware.
Error middleware has four parameters.
It centralizes error handling.
It is normally registered after routes.
How many parameters does Express identify as an error-handling middleware signature?
What is REST?
REST, or Representational State Transfer, is an architectural style for designing networked applications. RESTful APIs commonly model resources and use HTTP methods such as GET, POST, PUT, PATCH, and DELETE. Good REST API design also considers status codes, resource naming, statelessness, caching, and consistent representations.
REST is an architectural style.
REST APIs commonly model resources.
HTTP methods communicate intended operations.
Which HTTP method is conventionally used to retrieve a resource?
What is CORS?
CORS, or Cross-Origin Resource Sharing, is a browser security mechanism that controls whether a web page can make requests to a different origin. Servers can send CORS response headers specifying which origins, methods, headers, and credentials are allowed. CORS is primarily enforced by browsers and is not an authentication mechanism.
CORS means Cross-Origin Resource Sharing.
Browsers enforce CORS restrictions.
CORS is not a replacement for authentication or authorization.
What does CORS primarily control?