Native HMR in Node.js: A Technical Deep Dive

2025-06-04

This article details how to implement native Hot Module Replacement (HMR) in Node.js. Traditional approaches using `--watch` flags or virtual module systems like Vite are inefficient and suffer from module isolation. The author leverages Node.js's built-in `node:module` module hooks to create an incremental update mechanism based on file version numbers. The core is the `FileTree` class, which loads and monitors the file tree, and the `useTree` hook intercepts the module loading process, adding a version number to URLs for cache invalidation. The construction of a dependency tree ensures that when a dependent module changes, the parent module is also updated, resulting in efficient HMR and avoiding reevaluation of the entire module tree.

Read more
Development Module Loading

Hacking Node Module Hooks for Blazing Fast Frontend Dev

2025-05-07

To speed up frontend development, the author created a series of orthogonal Node module hooks. One module loader transforms JSX to JS, another remaps imports, another searches for .{ts,tsx,jsx} files when .js isn't found, and a final one works with FileTree, using query string cache busting to load the latest version of files. FileTree's update events trigger frontend rebuilds, with module versioning ensuring that modules are only re-executed if their dependencies change, preserving runtime state and avoiding restarting the entire process.

Read more
Development Module Hooks