80%
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.
timers —
setTimeout / setInterval callbackspending callbacks — some deferred system callbacks
poll — wait for and run I/O callbacks
check —
setImmediate callbacksTip
process.nextTick runs before promise microtasks.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.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.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.