Node.js Runtime & JavaScript Foundations
Core Node.js runtime concepts, modules, callbacks, events, and asynchronous context.
Q1: What is the global object in Node.js?
The global object is the top-level namespace in Node.js, providing built-in functions, variables, and objects (such as setTimeout, console, and Buffer) across all modules.
- Provides globally accessible built-in properties and methods.
- Equivalent to
windowin browser JavaScript environments.
What is the unified cross-platform global object reference introduced in modern JS?
Q2: What is the difference between CommonJS and ES Modules in Node.js?
CommonJS uses require() and module.exports synchronously at runtime. ES Modules use import and export statements parsed asynchronously at compile time.
- CommonJS loads modules synchronously using
require(). - ES Modules load modules using
import/exportsyntax.
Which file extension forces Node.js to treat a file as an ES Module by default?
Q3: How do environment variables work in Node.js?
Environment variables are exposed at runtime on the process.env global object. Libraries like dotenv load key-value pairs from .env files into process.env.
- Access environmental settings via
process.env.VAR_NAME. - Used to inject runtime configurations without hardcoding secrets.
What npm package is commonly used to load .env file variables into process.env?
Q4: What is the purpose of NODE_ENV environment variable?
NODE_ENV sets the execution environment (e.g., production, development). Libraries like Express optimize performance when set to production.
- Distinguishes development, testing, and production environments.
- Enables runtime performance optimizations when set to
production.
What optimization does Express perform when NODE_ENV is set to 'production'?
Q5: What is an error-first callback in Node.js?
An error-first callback passes an error object as its first parameter. If no error occurred, the first parameter is null or undefined.
- First argument is reserved for errors:
(err, result) => {}. - Standard convention in traditional asynchronous Node.js APIs.
In error-first callbacks, what is the value of the error parameter when execution succeeds?
Q6: What is util.promisify() in Node.js?
util.promisify() converts standard callback-based functions (following error-first conventions) into functions returning Promises.
- Transforms callback-style functions into Promise-returning functions.
- Enables clean
async/awaitsyntax integration.
What function signature must a callback follow to work with util.promisify()?
Q7: What is EventEmitter and how is it used?
EventEmitter is a core module class that allows objects to emit named events that trigger registered listener functions.
- Implements the publisher-subscriber design pattern in Node.js.
- Used heavily across built-in modules like HTTP and Streams.
Which method is used to attach a one-time event listener in EventEmitter?
Q8: What is the async_hooks module in Node.js?
The async_hooks core module tracks the lifecycle of asynchronous resources created within a Node.js application, useful for tracing context across async operations.
- Tracks creation and lifetime hooks for asynchronous resources.
- Powers diagnostic tracing and async local context storage.
Which core abstraction built on async_hooks maintains context across async operations?
Q9: How is AbortController used to cancel asynchronous operations in Node.js?
AbortController and AbortSignal are web standard APIs built into modern Node.js to gracefully cancel ongoing asynchronous operations (such as fetch HTTP requests, event listeners, file streams, or timers). Passing an AbortSignal to asynchronous APIs allows calling controller.abort() from anywhere in the code to reject pending promises immediately and release underlying resources.
AbortControllerprovides a standardized mechanism to cancel active asynchronous tasks.- Supported natively across Node.js core modules like
fs,http,events, andtimers.
What property of AbortController is passed to asynchronous APIs to listen for cancellation events?
Q10: What was the Domain module in Node.js, why was it deprecated, and what replaced it?
The domain module was designed to handle unhandled errors across multiple asynchronous operations by binding error handlers to execution contexts. It was deprecated because its implicit context binding led to subtle memory leaks, unpredictable state corruption, and severe performance overhead across the asynchronous call stack. It has been replaced by structured try/catch blocks with async/await, explicit error propagation patterns, and AsyncLocalStorage from the async_hooks module for implicit context tracking.
domainwas deprecated due to memory leaks, state corruption, and execution performance overhead.- Modern Node.js uses
AsyncLocalStorageand explicitasync/awaiterror handling as robust replacements.
Which module in modern Node.js provides thread-safe, asynchronous context propagation without the pitfalls of Domains?