Node.js 22 LTS excels in production for: RESTful APIs (Express, Fastify, NestJS handle high-traffic microservices), real-time applications (WebSocket chat, live notifications, collaborative tools with native ws module), serverless functions (AWS Lambda, Vercel, Netlify with fast cold starts), data streaming (event-driven architecture for logs, video/audio, IoT), CLI tools and DevOps automation (built-in test runner, watch mode). Major adopters: Netflix, PayPal, LinkedIn, Walmart. Strengths: largest ecosystem (npm 2M+ packages), mature tooling, excellent I/O performance. 2025 position: proven stability vs Bun's speed and Deno's security, remains optimal for enterprise JavaScript backends.
Node.js FAQ & Answers
70 expert Node.js answers researched from official documentation. Every answer cites authoritative sources you can verify.
unknown
70 questionsThe event loop is Node's mechanism for non-blocking I/O despite single-threaded JavaScript execution. Six phases: timers (setTimeout/setInterval), pending callbacks (I/O callbacks deferred), idle/prepare (internal), poll (retrieve new I/O events), check (setImmediate callbacks), close callbacks (socket.on('close')). Between phases, microtask queues execute (process.nextTick(), then Promises). Worker pool (libuv threadpool) handles blocking operations: fs, crypto, DNS, zlib. Never block event loop with synchronous operations or CPU-intensive tasks without offloading to worker threads.
process.nextTick() executes callback immediately after current operation, before event loop continues to any phase. setImmediate() executes callback in check phase, after I/O events. Execution order: current operation → process.nextTick() queue → Promise microtasks → event loop phases → setImmediate(). Critical difference: process.nextTick() can starve I/O if used recursively (entire queue must empty), setImmediate() processes one callback per loop iteration. Use setImmediate() for I/O-related callbacks, process.nextTick() only when callback must execute before event loop continues.
Promises represent eventual completion/failure of asynchronous operations. Three states: pending, fulfilled (resolved with value), rejected (failed with error). Creation: new Promise((resolve, reject) => { /* async work */ }). Chaining: promise.then(onFulfilled).catch(onRejected).finally(onFinally). Combinators: Promise.all([p1, p2]) (all succeed or any reject), Promise.allSettled([p1, p2]) (wait for all regardless), Promise.race([p1, p2]) (first to settle), Promise.any([p1, p2]) (first to fulfill). Promises are immutable once settled. Node.js APIs provide Promise versions: fs.promises, timers/promises.
async/await is syntactic sugar for Promises enabling synchronous-looking asynchronous code. async keyword: declares function returning Promise automatically. await keyword: pauses execution until Promise settles, only inside async functions or ES module top-level (Node.js 14.8+). Example: async function fetch() { const data = await readFile('file.txt'); return process(data); }. Error handling: try/catch blocks. Parallel execution: const [a, b] = await Promise.all([fetchA(), fetchB()]). Behind scenes: compiles to Promise chains with generator functions. Always return values from async functions (wrapped in resolved Promise).
Node.js supports two module systems: ES Modules (ESM, modern standard) and CommonJS (legacy). Enable ESM: add "type": "module" to package.json, use .mjs extension, or use import/export syntax. CommonJS uses require()/module.exports. Module resolution: node_modules lookup, file extensions (.js, .json, .node, .mjs, .cjs), index files, package.json exports/main fields. Module caching: first load executes and caches, subsequent loads return cached. Node.js 20+ can require() ESM modules (except those with top-level await). Use ESM for new projects.
Enable ES modules: add "type": "module" to package.json or use .mjs extension. Import syntax: import { named } from './module.js'; import defaultExport from './module.js'; import * as ns from './module.js'. Export syntax: export const value = 42; export default function() {}; export { a, b }. File extensions required in imports. Top-level await supported (no async wrapper needed). Dynamic imports: const module = await import('./dynamic.js'). Interop: Node.js 20.12+ can require() ESM. Use node: prefix for built-ins: import { readFile } from 'node:fs/promises'.
npm (Node Package Manager) is Node.js's default package manager with 2M+ packages. Core commands: npm install