80%
Node.js Runtime, event loop, modules, streams, scaling and npm0/25 questions known
1. What Node.js is
DefinitionNode.js is a JavaScript runtime built on V8 + libuv.
FlowFlowRequest→Event loop (1 JS thread)→libuv: OS async I/O + thread pool (fs, crypto, dns)→Callback queued→Response
Great for I/O-heavy work: APIs, real-time apps, proxies.
Bad at long CPU work on the main thread (image processing, big loops) — it blocks every request.
2. The Node event loop phases
timers — setTimeout / setInterval callbacks
pending callbacks — some deferred system callbacks
poll — wait for and run I/O callbacks
check — setImmediate callbacks
Tipprocess.nextTick runs before promise microtasks.
3. Modules and npm
require() / module.exports: import / export
Loaded synchronously at runtime: Static, analysable, top-level await
Default for .js without "type": "module": .mjs, or "type": "module" in package.json
package.json: scripts, dependencies vs devDependencies, engines.
Semver MAJOR.MINOR.PATCH: ^1.2.3 allows minor + patch, ~1.2.3 allows patch only.
Commit package-lock.json; use npm ci in CI for exact, repeatable installs.
npx runs a package binary without installing it globally.
4. Core modules, streams and errors
Core modules: fs/promises, path, http, crypto, events, stream, os, child_process, worker_threads.
Streams process data chunk by chunk (Readable, Writable, Duplex, Transform) — use pipeline() for errors and back-pressure.
EventEmitter: on, once, emit — the base of streams and servers.
Always handle promise rejections; log unhandledRejection / uncaughtException, then exit and let the process manager restart.
Common mistakeSync APIs in request handlers (readFileSync, bcrypt.hashSync, big JSON.parse loops) block every other user.
5. Scaling and production
cluster / PM2: one process per CPU core behind a shared port
worker_threads: run CPU-heavy work off the main thread
Load balancer + stateless app: scale horizontally across machines
Redis: shared cache, sessions, rate limits, queues (BullMQ)
Config from environment variables (12-factor); never commit secrets.
Structured logs (pino), health checks (/health), metrics.
Cron jobs: node-cron in a single instance or a queue scheduler to avoid duplicates when scaled.
Most asked (8)
QWhat is Node.js, and how can it be single-threaded yet handle many requests?Node.js is a JavaScript runtime built on Chrome's V8 engine, plus libuv, a C library that provides the event loop, async I/O and a small…
QWalk me through the phases of the Node.js event loop.Each turn of the loop goes through phases, each with its own queue of callbacks: - timers: expired setTimeout / setInterval callbacks -…
Qprocess.nextTick vs setImmediate vs setTimeout(fn, 0)?- process.nextTick(fn): runs right after the current operation, before promise callbacks and before the loop continues.
QWhat is the difference between blocking and non-blocking code? How do you avoid blocking the event loop?Blocking code makes the single JavaScript thread wait until it finishes, so no other request, timer or callback can run.
QCommonJS vs ES modules in Node: what are the differences?CommonJS (require / module.exports) is Node's original system.
QWhat are streams, and why use them?A stream processes data piece by piece (chunks) instead of loading it all into memory.
QHow do you handle errors in Node, including uncaught exceptions and unhandled rejections?Handle errors where you can recover: - sync code and await: try/catch - promises: .catch() - callbacks: check the error-first argument…
QWorker threads vs cluster vs child_process: when do you use each?- worker_threads: extra JavaScript threads in the same process.