Skip to content

Node.js Cheatsheet — Complete Quick Reference (2026)

DodaTech Updated 2026-06-20 3 min read

In this tutorial, you'll learn about Node.js Cheatsheet. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Node.js is a JavaScript runtime built on Chrome's V8 Engine, providing an event-driven, non-blocking I/O model for building scalable server-side applications.

Module System (CommonJS & ESM)

// CommonJS
const fs = require('fs')
module.exports = { myFn }

// ESM (package.json: "type": "module")
import fs from 'fs'
export const myFn = () => {}

File System (fs)

Method Description
fs.readFile(path, cb) Read file async
fs.writeFile(path, data, cb) Write file async
fs.appendFile(path, data, cb) Append to file
fs.unlink(path, cb) Delete file
fs.mkdir(path, cb) Create directory
fs.readdir(path, cb) List directory
fs.stat(path, cb) File info
fs.watch(path, cb) Watch for changes
fs.createReadStream(path) Readable stream
fs.createWriteStream(path) Writable stream

Promises API: import fs from 'fs/promises'await fs.readFile(path, 'utf8')

Streams

const rs = fs.createReadStream('input.txt', { highWaterMark: 64 * 1024 })
const ws = fs.createWriteStream('output.txt')
rs.pipe(ws)   // pipe readable → writable

// Transform stream
const { Transform } = require('stream')
const upper = new Transform({ transform(chunk, enc, cb) { cb(null, chunk.toString().toUpperCase()) } })
rs.pipe(upper).pipe(ws)

Buffers

const buf = Buffer.from('hello', 'utf8')   // <Buffer 68 65 6c 6c 6f>
buf.toString()                              // 'hello'
buf.length                                  // 5 bytes
Buffer.alloc(1024)                          // zero-filled
Buffer.concat([buf1, buf2])                 // combine

Events (EventEmitter)

const EventEmitter = require('events')
const ee = new EventEmitter()
ee.on('event', (data) => console.log(data))
ee.emit('event', 'hello')
ee.once('once', () => {})    // fires once
ee.removeAllListeners('event')

Child Processes

const { exec, spawn, fork } = require('child_process')
exec('ls -la', (err, stdout, stderr) => {})
const child = spawn('node', ['script.js'], { stdio: 'inherit' })
child.on('exit', (code) => {})

Cluster

const cluster = require('cluster')
if (cluster.isPrimary) {
  for (let i = 0; i < os.cpus().length; i++) cluster.fork()
  cluster.on('exit', (worker) => cluster.fork())
} else {
  // worker: create server
}

HTTP Server

const http = require('http')
const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' })
  res.end(JSON.stringify({ status: 'ok' }))
})
server.listen(3000)

Process Methods

Method Purpose
Process.argv CLI arguments
Process.env Environment variables
Process.cwd() Current working directory
Process.exit(code) Exit with code
Process.on('uncaughtException', cb) Handle crashes
Process.memoryUsage() Memory stats
Process.nextTick(cb) Defer to next tick

Must-Know Items

  • Use fs.promises API for modern async/await code
  • Streams prevent memory overflow on large files — always prefer pipe() over loading into memory
  • Buffer.alloc() is safer than Buffer.from() for fixed-size zero-filled buffers
  • cluster.fork() creates child processes sharing server ports (round-robin on Linux)
  • Process.nextTick() runs before I/O; setImmediate() runs after I/O
  • Always handle uncaughtException and unhandledRejection in production
  • npm init and package.json are the foundation of every Node project

{{< faq "What is the difference between CommonJS and ES modules in Node.js?">}}CommonJS uses require() / module.exports and is synchronous. ES modules use import / export and are asynchronous with Static Analysis. Use "type": "module" in package.json for ESM, or .mjs extension.{{< /faq >}}

Why use streams instead of reading the whole file?

Streams Process data in chunks without loading the entire file into memory. For large files, this prevents high memory usage and allows backpressure handling. The pipe() method automatically manages flow control.

See full Node.js tutorials for advanced server patterns.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro