Registry / testing / hot-patcher

hot-patcher

JSON →
library2.0.1jsnpmunverified

Hot-Patcher is a JavaScript/TypeScript library designed to manage method overrides and patching across different environments, such as Node.js, web browsers, and React Native. It provides a consistent API for dynamically replacing or augmenting existing methods, which is particularly useful for adapting code behavior to specific runtime contexts or for plugin architectures. The library is currently at version 2.0.1 and focuses on offering a "single agreed-upon method" for handling these patches, addressing common challenges faced when managing conditional method implementations. While a specific release cadence is not stated, the project appears to be actively maintained. Key differentiators include its explicit support for chaining and sequencing functions via a plugin API, as well as clear mechanisms for restoring methods to their original patched state. It enforces an ESM-only architecture, which is a significant consideration for integration into existing projects.

npm install hot-patcher
INSTALL
IMPORT
SIG · HOT-PATCHER
H
hot-patcher
testingjavascriptv2.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

HotPatcher
import { HotPatcher } from 'hot-patcher';
const { HotPatcher } = require('hot-patcher');
Hot-Patcher is an ESM-only library since v2.0.0. CommonJS require() statements will fail.
HotPatcher (Type)
import type { HotPatcher } from 'hot-patcher';
Standard type-only import for use in TypeScript projects.
PatchMethod
import type { PatchMethod } from 'hot-patcher';
Import the type for a patched method function signature for better type safety when defining patches.

Demonstrates how to instantiate `HotPatcher`, wrap an existing function with `patchInline` for initial registration, apply a new patch, and then restore the original patched function.

import { HotPatcher } from "hot-patcher"; // Instantiate the patcher const patcher = new HotPatcher(); // Define an original function (could be a method on a class) function calculate(a: number, b: number): number { console.log("Original calculate called"); return a + b; } // Wrap the original function with patchInline for initial execution and registration function add(a: number, b: number): number { return patcher.patchInline("addNumbers", (numA, numB) => calculate(numA, numB), a, b); } console.log("Initial call (should use original logic):", add(5, 3)); // Now, patch the 'addNumbers' method to multiply instead patcher.patch("addNumbers", (numA: number, numB: number) => { console.log("Patched calculate called, multiplying instead!"); return numA * numB; }); console.log("After patching (should use patched logic):", add(5, 3)); // Using execute directly console.log("Direct execute after patching:", patcher.execute("addNumbers", 10, 2)); // Restore the method to its originally patched function patcher.restore("addNumbers"); console.log("After restoring (should use original logic again):", add(5, 3));
Debug
Known issues
breakingHot-Patcher is an ESM-only library starting from version 2.x. This means it can only be imported using `import` statements and will not work with CommonJS `require()` in Node.js environments unless transpiled or bundled.
fix
Convert your project to ESM (e.g., set `"type": "module"` in `package.json`) and use `import` statements, or ensure your bundler (like Webpack or Rollup) correctly handles ESM imports into a CJS output.
affects: >=2.0.0
gotchaCalling `patch(name, fn)` without the `{ chain: true }` option will overwrite all existing chained methods for that `name` with the new function `fn`.
fix
To add a function to an existing chain without overwriting, always specify `{ chain: true }` in the options parameter: `patcher.patch("methodName", myFn, { chain: true });`.
affects: >=2.0.0
gotchaThe `patchInline` method will register and immediately execute a method, marking it as patched. In contrast, `patch` only registers the method without immediate execution.
fix
Use `patchInline` when you want to define the base behavior of a function that can be subsequently patched, allowing it to execute upon wrapping. Use `patch` when you are purely adding or overriding a method's definition without needing to wrap an existing function's implementation immediately.
affects: >=2.0.0
Errors
Common errors & fixes
Error [ERR_REQUIRE_ESM]: require() of ES Module ...hot-patcher/dist/index.js from ... not supported.
Attempting to `require()` the `hot-patcher` package in a CommonJS context, but the library is ESM-only.
fix
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import { HotPatcher } from 'hot-patcher';` instead of `const HotPatcher = require('hot-patcher');`.
TypeError: hot_patcher_1.HotPatcher is not a constructor
Incorrect import statement (e.g., a default import instead of a named import) or attempting to instantiate a non-constructor.
fix
Verify that `HotPatcher` is imported as a named export: `import { HotPatcher } from 'hot-patcher';`. If using TypeScript, ensure `esModuleInterop` is enabled if you are trying to use `require` with a transpiler.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
hot-patcher — npm install hot-patcher · libregistry