{"id":11044,"library":"hot-patcher","title":"Hot Method Patcher","description":"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.","status":"active","version":"2.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/perry-mitchell/hot-patcher","tags":["javascript","patch","override","hot","typescript"],"install":[{"cmd":"npm install hot-patcher","lang":"bash","label":"npm"},{"cmd":"yarn add hot-patcher","lang":"bash","label":"yarn"},{"cmd":"pnpm add hot-patcher","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Hot-Patcher is an ESM-only library since v2.0.0. CommonJS require() statements will fail.","wrong":"const { HotPatcher } = require('hot-patcher');","symbol":"HotPatcher","correct":"import { HotPatcher } from 'hot-patcher';"},{"note":"Standard type-only import for use in TypeScript projects.","symbol":"HotPatcher (Type)","correct":"import type { HotPatcher } from 'hot-patcher';"},{"note":"Import the type for a patched method function signature for better type safety when defining patches.","symbol":"PatchMethod","correct":"import type { PatchMethod } from 'hot-patcher';"}],"quickstart":{"code":"import { HotPatcher } from \"hot-patcher\";\n\n// Instantiate the patcher\nconst patcher = new HotPatcher();\n\n// Define an original function (could be a method on a class)\nfunction calculate(a: number, b: number): number {\n    console.log(\"Original calculate called\");\n    return a + b;\n}\n\n// Wrap the original function with patchInline for initial execution and registration\nfunction add(a: number, b: number): number {\n    return patcher.patchInline(\"addNumbers\", (numA, numB) => calculate(numA, numB), a, b);\n}\n\nconsole.log(\"Initial call (should use original logic):\", add(5, 3));\n\n// Now, patch the 'addNumbers' method to multiply instead\npatcher.patch(\"addNumbers\", (numA: number, numB: number) => {\n    console.log(\"Patched calculate called, multiplying instead!\");\n    return numA * numB;\n});\n\nconsole.log(\"After patching (should use patched logic):\", add(5, 3));\n\n// Using execute directly\nconsole.log(\"Direct execute after patching:\", patcher.execute(\"addNumbers\", 10, 2));\n\n// Restore the method to its originally patched function\npatcher.restore(\"addNumbers\");\nconsole.log(\"After restoring (should use original logic again):\", add(5, 3));","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Hot-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.","severity":"breaking","affected_versions":">=2.0.0"},{"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 });`.","message":"Calling `patch(name, fn)` without the `{ chain: true }` option will overwrite all existing chained methods for that `name` with the new function `fn`.","severity":"gotcha","affected_versions":">=2.0.0"},{"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.","message":"The `patchInline` method will register and immediately execute a method, marking it as patched. In contrast, `patch` only registers the method without immediate execution.","severity":"gotcha","affected_versions":">=2.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"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');`.","cause":"Attempting to `require()` the `hot-patcher` package in a CommonJS context, but the library is ESM-only.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module ...hot-patcher/dist/index.js from ... not supported."},{"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.","cause":"Incorrect import statement (e.g., a default import instead of a named import) or attempting to instantiate a non-constructor.","error":"TypeError: hot_patcher_1.HotPatcher is not a constructor"}],"ecosystem":"npm"}