API Reference

proxyquire

Overrides require dependencies during unit tests without modifying the module under test.

Official Documentation ↗
CompatibilityThis documentation covers version v2.1. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev proxyquire

Methods

proxyquire loads a module while substituting its imports, making isolation possible without a DI framework.

Key Features

  • Per-test dependency stubbing
  • noCallThru controls passthrough behavior
  • Works with CommonJS require graphs

Usage Notes

Pin a version and check the official docs for the API surface you depend on before upgrading.

API reference evidence

Fetched from official sources on 2026-11-08T17:51:50+08:00 via WebFetch https://raw.githubusercontent.com/thlorenz/proxyquire/master/README.md.

FAQ

Why did my stub not take effect?
The stub key must match the exact module string used in require, including relative paths. A mismatch silently keeps the real dependency.
Does it work with ESM?
No. proxyquire relies on CommonJS require. For ESM use dependency injection or module mocking from the test runner instead.
When should I avoid proxyquire?
When the module can be designed for injection instead. Explicit parameters are clearer than substituting an import graph.