CommonJS dependency control

@stackline/proxyquire

Load a CommonJS subject with focused dependency stubs, preserving the familiar Proxyquire API while making caller anchoring, cache cleanup, loader composition, types, and releases explicit.

npm install --save-dev @stackline/proxyquire
v1.0.0Node 12-24TypeScript 3.9+Exact-pinned resolver

Interactive contract model

Stub-resolution workbench

This browser tool previews object-stub call-through rules. Actual module interception runs only in Node's CommonJS loader.

JSON preview

JSON preview
Call-through fills omitted properties The stub replaces send and inherits format from the original dependency.
{}
Local stubs apply to the subject's matching require.Native ESM imports are outside this model.

Established API

Callable, configurable, caller-relative.

The default export remains a function. Configuration methods return that same instance for chaining.

APIBehaviorStatus
proxyquire(request, stubs)Load a CommonJS subject with matching dependency stubs.Preserved
.load(request, stubs)Equivalent named method.Preserved
.noCallThru() / .callThru()Control whether omitted stub properties use the original dependency.Preserved
.noPreserveCache() / .preserveCache()Control post-load eviction or restoration of the pre-call cache entry.Preserved
createProxyquire(from) / from(from)Create an independent caller-anchored instance.Additive

Honest ESM boundary

ESM host, CommonJS subject.

An ESM or native-TypeScript test can anchor CommonJS resolution explicitly. Proxyquire does not rewrite native ESM import bindings.

import { createProxyquire } from '@stackline/proxyquire'

const proxyquire = createProxyquire(import.meta.url)
const subject = proxyquire('./subject.cjs', {
  './dependency.cjs': { value: 'stubbed' }
})
Read the complete compatibility boundary

Release gate

Test the global state, not only the result.

The release gate covers cache snapshots, transient children, nested loads, global-state isolation, loader replacement, packaging, and clean consumers.

7
Node major lines from 12 through 24
3
operating systems in CI
1
exact-pinned direct runtime dependency
2
TypeScript generations checked
CJS
explicit interception boundary
Alias
legacy package-name smoke install

Drop-in adoption

Keep existing requires.

An npm alias changes the package without changing require('proxyquire').

npm install --save-dev proxyquire@npm:@stackline/proxyquire

Project resources

Inspect the contract.