jestjs/jest · error · DuplicateHasteCandidatesError
The name `${name}` was looked up in the Haste module map. It
Error message
The name `${name}` was looked up in the Haste module map. It cannot be resolved, because there exists several different files, or packages, that provide a module for that particular name and platform. ${platformMessage} You must delete or exclude files until there remains only one of these:
* `${dupFilePath}` (${getTypeMessage(dupFileType)})
What it means
jest-haste-map builds a haste-id → file map for platforms/generic resolution. When a single haste name resolves to more than one file for the same platform, `_assertNoDuplicates` throws DuplicateHasteCandidatesError (ModuleMap.ts:191) listing every conflicting path, because the resolver cannot pick one deterministically. This is the classic Jest 'duplicate manual mocks / haste collision' failure.
Source
Thrown at packages/jest-haste-map/src/ModuleMap.ts:191
private _assertNoDuplicates(
name: string,
platform: string,
supportsNativePlatform: boolean,
relativePathSet: DuplicatesSet | null,
) {
if (relativePathSet == null) {
return;
}
// Force flow refinement
const previousSet = relativePathSet;
const duplicates = new Map();
for (const [relativePath, type] of previousSet) {
const duplicatePath = fastPath.resolve(this._raw.rootDir, relativePath);
duplicates.set(duplicatePath, type);
}
throw new DuplicateHasteCandidatesError(
name,
platform,
supportsNativePlatform,
duplicates,
);
}
static create(rootDir: string): ModuleMap {
return new ModuleMap({
duplicates: new Map(),
map: new Map(),
mocks: new Map(),
rootDir,
});
}
}
class DuplicateHasteCandidatesError extends Error {View on GitHub (pinned to f49721c78e)
Solutions
- Read the listed duplicate paths and delete or rename one of them.
- Add `modulePathIgnorePatterns` / ` haste.providesModuleNodeModules` / `roots` config so only one copy is crawled.
- If a manual mock collides with a real package, remove the `__mocks__/<name>.js` entry or scope it via configuration.
- For monorepos, ensure each package's `__mocks__` is not globally visible; use `projects`/workspaces config to isolate.
- If a symlink is the cause, dedupe with yarn `resolutions` or npm `overrides`.
Example fix
// before: both __mocks__/db.js and node_modules/db exist
testEnvironment: 'node'
// after: remove the global manual mock and scope it
// delete __mocks__/db.js, instead mock at test level:
jest.mock('db', () => ({ query: jest.fn() })); Defensive patterns
Strategy: validation
Validate before calling
// preflight: scan for haste name collisions before running
const seen = new Map();
for (const f of crawl('src')) {
const name = readProvidesModuleName(f);
if (name && seen.has(name)) throw new Error(`duplicate haste id ${name}: ${seen.get(name)} vs ${f}`);
if (name) seen.set(name, f);
} Type guard
const hasNoHasteCollision = (map: Map<string, string>) => map.size === new Set(map.values()).size;
Prevention
- Avoid @providesModule except where necessary; prefer explicit imports.
- Scope __mocks__ per project in monorepos.
- Configure modulePathIgnorePatterns to exclude duplicate roots.
- Dedupe node_modules via resolutions/overrides.
When it happens
Trigger: Two files declare the same haste module name (e.g. `@providesModule Foo`) for the same platform; a manual mock in `__mocks__` collides with a real module of the same name; an npm dependency and a local file share a haste id; platform-specific files (`.ios.js`/`.android.js`) collide on the generic platform.
Common situations: Adding a manual mock that duplicates an installed package name; monorepo with two packages providing the same module; symlinking node_modules so a package is crawled twice; leftover generated files sharing a name; RN-style haste modules colliding after a dependency addition.
Related errors
- Cannot find module '${presetPath}'
- Could not resolve a module for a custom reporter. Module n
- haste.enableSymlinks is incompatible with watchman
- Duplicated files or mocks. Please check the console for more
- Cannot resolve module '${moduleName}' from paths ['${paths}'
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/4f5ea2458c0761e1.json.
Report an issue: GitHub.