Skip to content
Latchkey

Jest "Cannot find module ... from <test>" - Resolve Imports in CI

Jest could not resolve a module imported by a test file. The package is not installed, the relative path is wrong (often a case mismatch that only matters on Linux), or a path alias is missing from the Jest config.

What this error means

A spec fails at collection with Cannot find module 'X' from 'src/foo.test.ts' and a require stack. It often passes on macOS but fails on a case-sensitive Linux runner, or only after CI skips devDependencies.

jest
Cannot find module '../helpers/db' from 'src/users.test.ts'

  Require stack:
    src/users.test.ts
    at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js)

Common causes

Wrong relative path or case mismatch

Importing ../helpers/DB when the file is db.ts resolves on case-insensitive macOS/Windows but fails on a case-sensitive Linux CI runner.

Path alias not mapped in Jest

A @/... alias resolves via your bundler and tsconfig paths, but Jest resolves modules itself and needs the same mapping in moduleNameMapper.

Dependency missing in CI

The module is a devDependency but CI ran npm ci --omit=dev, or it was never added to package.json, so it is absent from node_modules.

How to fix it

Map path aliases in Jest config

Mirror your tsconfig paths into moduleNameMapper so Jest resolves aliases exactly as the build does.

jest.config.js
// jest.config.js
module.exports = {
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1',
  },
};

Fix the path case and install missing deps

  1. Compare each failing import against the real filename, including capitalization.
  2. Rename the import or file so they match exactly on a case-sensitive filesystem.
  3. Run npm ci (without --omit=dev) so test-only dependencies are present.

How to prevent it

  • Keep moduleNameMapper in sync with tsconfig paths.
  • Lint with eslint-plugin-import (import/no-unresolved) to catch bad paths.
  • Develop on a case-sensitive filesystem to surface case bugs before CI.

Frequently asked questions

What causes ""Cannot find module ... from""?
Importing ../helpers/DB when the file is db.ts resolves on case-insensitive macOS/Windows but fails on a case-sensitive Linux CI runner.
How do I fix "Cannot find module ... from"?
Mirror your tsconfig paths into moduleNameMapper so Jest resolves aliases exactly as the build does.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card