Node.js Runtime Essentials
Run JavaScript and TypeScript on the server, use core modules, manage configuration and understand what blocks the event loop.
Intermediate · about 6 hours · difficulty 2/5
Learn first: Asynchronous JavaScript and fetch, TypeScript Fundamentals, HTTP, Requests and Responses
Node.js runs JavaScript outside the browser. It powers Express, Fastify and the server side of Next.js, so its behaviour is your backend's behaviour.
Essentials
- Run scripts with node, and TypeScript in development with tsx. Use the current LTS version.
- Core modules: fs/promises for files, path, crypto for hashing and random tokens, and the built-in fetch.
- process.env for configuration, loaded from .env files in development and from the platform in production.
- ES modules vs CommonJS: prefer ESM ("type": "module") for new projects, but recognise require() in older code.
Performance model
Node handles many concurrent requests on one thread because I/O is non-blocking. CPU-heavy work (resizing images, generating large PDFs, hashing with high cost in a loop) blocks every other request. Move such work to a background job or a worker thread.
Reliability
- Handle unhandledRejection and uncaughtException by logging and exiting; let the platform restart the process.
- Implement graceful shutdown: on SIGTERM, stop accepting requests, finish in-flight ones, close the database pool.
- Use a file watcher (node --watch or tsx watch) in development.
Pitfalls
- Synchronous fs calls inside request handlers.
- Reading process.env all over the codebase instead of validating it once at startup.
- Running an odd-numbered or end-of-life Node version in production.
You are ready to move on when you can write a small Node.js HTTP server with the built-in http module, read config from validated env vars, and shut it down gracefully.
Key concepts
- LTS versions and nvm
- Core modules: fs, path, crypto
- process.env and .env files
- ESM vs CommonJS
- Blocking vs non-blocking work
- Graceful shutdown
- Running TypeScript with tsx
- Worker threads
Mini project
Build a tiny URL shortener using only Node's built-in http module and a JSON file for storage, with env-based config validated by Zod and graceful shutdown on SIGTERM.
Check yourself
One endpoint generates a large PDF synchronously and the whole API becomes slow for everyone. Why, and what are two fixes?
Free resources
- Learn Node.js (Node.js)
- Node.js API Documentation (Node.js)
- Node.js Roadmap (roadmap.sh)