Skip to content
← All cheat sheets

Cheat sheet

Node.js

The event loop, streams, and backend patterns that come up in system questions.

Event loop

  • Single-threaded

    One JS thread; I/O is offloaded to libuv's thread pool.

  • Phase order

    timers → pending → poll → check → close
  • process.nextTick

    Runs before promises, before the next phase — can starve the loop.

  • Blocking

    CPU-heavy sync code blocks everything; offload to workers.

Modules

  • CommonJS

    const x = require('x'); module.exports = …
  • ESM

    import x from 'x'; export default …

    "type":"module" or .mjs.

  • __dirname in ESM

    import.meta.url

    No __dirname; derive from import.meta.url.

Streams & buffers

  • Why streams

    Process data in chunks — constant memory for big payloads.

  • Backpressure

    pipe()/pipeline() handle it; manual writes must respect .write() returning false.

  • Buffer

    Raw bytes; specify encoding when converting to string.

Patterns

  • Error-first callbacks

    (err, data) => {}
  • Env config

    process.env.PORT

    Never hardcode secrets.

  • Graceful shutdown

    Listen for SIGTERM, drain connections, then exit.

Aevrofy · aevrofy.com — free interview prep