HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
10 Topics
Chapter•30 Minutes

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.

Key Takeaways
  • Provides globally accessible built-in properties and methods.
  • Equivalent to window in browser JavaScript environments.
Knowledge Check

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.

Key Takeaways
  • CommonJS loads modules synchronously using require().
  • ES Modules load modules using import/export syntax.
Knowledge Check

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.

Key Takeaways
  • Access environmental settings via process.env.VAR_NAME.
  • Used to inject runtime configurations without hardcoding secrets.
Knowledge Check

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.

Key Takeaways
  • Distinguishes development, testing, and production environments.
  • Enables runtime performance optimizations when set to production.
Knowledge Check

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.

Key Takeaways
  • First argument is reserved for errors: (err, result) => {}.
  • Standard convention in traditional asynchronous Node.js APIs.
Knowledge Check

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.

Key Takeaways
  • Transforms callback-style functions into Promise-returning functions.
  • Enables clean async/await syntax integration.
Knowledge Check

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.

Key Takeaways
  • Implements the publisher-subscriber design pattern in Node.js.
  • Used heavily across built-in modules like HTTP and Streams.
Knowledge Check

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.

Key Takeaways
  • Tracks creation and lifetime hooks for asynchronous resources.
  • Powers diagnostic tracing and async local context storage.
Knowledge Check

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.

Key Takeaways
  • AbortController provides a standardized mechanism to cancel active asynchronous tasks.
  • Supported natively across Node.js core modules like fs, http, events, and timers.
Knowledge Check

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.

Key Takeaways
  • domain was deprecated due to memory leaks, state corruption, and execution performance overhead.
  • Modern Node.js uses AsyncLocalStorage and explicit async/await error handling as robust replacements.
Knowledge Check

Which module in modern Node.js provides thread-safe, asynchronous context propagation without the pitfalls of Domains?