Skip to content

Cannot Find Module

Error message

                Cannot find module './utils' or its corresponding type declarations
              

Quick summary

Jest can't find the module you're trying to import. The path might be wrong, the file might not exist, or Jest's module resolution isn't configured correctly.

Why this happens

  • The import path is wrong — typo in the file path or incorrect relative path.

  • The module file doesn't exist or has a different name.

  • Jest's moduleNameMapper is not configured for path aliases.

  • The file extension doesn't match what Jest expects.

Minimal example

✗ Broken code
// File is at ./src/utils.js but imported as ./utils
const { add } = require('./utils'); // Cannot find module
✓ Fixed code
// Correct path
const { add } = require('./src/utils');

Fix the import path to match the actual file location.

How to diagnose

  • Check the import path — is the file actually at that location?

  • Verify the file exists and has the correct name.

  • If using path aliases (like @/utils), check Jest's moduleNameMapper config.

  • Check if the file extension is correct (.js, .ts, .jsx, .tsx).

How to fix

  • Fix the import path to point to the correct file location.

  • Create the missing module file.

  • Configure moduleNameMapper in jest.config.js for path aliases.

  • Ensure the file extension matches Jest's testMatch or moduleFileExtensions config.

How to prevent

  • Use consistent import paths and path aliases.

  • Configure Jest's moduleNameMapper to match your build tool's path aliases.

Related Resources

Related Lessons

← Back to language