Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript cannot find module './logo.png' or its type declarations

ErrorCannot find module './logo.png' or its corresponding type declarations.

Short answer

The bundler can load an image, but TypeScript knows nothing about files that are not code. It needs a declaration for the extension. With Vite, reference vite/client. Otherwise add a .d.ts that declares the module, and make sure tsconfig includes that file. Jest needs its own mapping for images.

  • Applies to TypeScript 5.5, Vite 7, Jest 30
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out whether any declaration file for assets exists and whether tsconfig loads it.

    ls src/*.d.ts && grep -nE "include|types" tsconfig.json
  2. 2Match what you see, then jump to its fix

    1. 1TS2307 on a .png, .svg or .jpg import in a Vite projectNo asset types are loaded (Vite)Fix
    2. 2The project does not use Vite, or you added a .d.ts and the error is still thereCustom declaration is missing, or tsconfig does not include itFix
    3. 3Cannot find module './logo.png' from 'src/App.tsx', coming from Jest and not tscJest cannot resolve the image at test timeFix

What does the error look like?

  • Cannot find module './logo.png' or its corresponding type declarations.
  • TS2307: Cannot find module '../assets/hero.svg' or its corresponding type declarations.
  • Cannot find module './logo.png' from 'src/App.tsx' (Jest)

Cause 1 of 3

No asset types are loaded (Vite)

1Check

Vite ships the types for asset imports in vite/client. They only load if a file references them or tsconfig lists them.

grep -rn "vite/client" src/ tsconfig*.json

2Fix

Create a declaration file with a triple-slash reference. Alternatively list vite/client in compilerOptions.types.

// src/vite-env.d.ts
/// <reference types="vite/client" />

3Verify

Type-check the project.

npx tsc --noEmit

Confirmed by vite.dev

Did this fix it?

Cause 2 of 3

Custom declaration is missing, or tsconfig does not include it

1Check

A declaration only helps if the compiler reads the file. With compilerOptions.types set, only the listed packages enter the global scope, and the file must sit under an include path.

cat tsconfig.json

2Fix

Declare each extension as a module with a default export, and list the file under include if it lives outside src.

// src/assets.d.ts
declare module '*.png' {
  const src: string;
  export default src;
}

// tsconfig.json
{ "include": ["src", "./assets.d.ts"] }

3Verify

Restart the TypeScript server in your editor, then type-check.

npx tsc --noEmit

Confirmed by vite.dev

Did this fix it?

Cause 3 of 3

Jest cannot resolve the image at test time

1Check

Jest runs files through its own transform and resolver, so TypeScript declarations do not help it. By default it tries to load the image as code.

npx jest --showConfig | grep -A4 moduleNameMapper

2Fix

Map image files to a small stub module with moduleNameMapper.

// jest.config.js
module.exports = {
  moduleNameMapper: {
    '^[./a-zA-Z0-9$_-]+\\.png$': '<rootDir>/test/imageStub.js',
  },
};

// test/imageStub.js
module.exports = 'test-file-stub';

3Verify

Run the test that renders the component.

npx jest

Confirmed by jestjs.io

Did this fix it?

How do I stop it coming back?

  • Keep one assets declaration file, and list every asset extension you import (png, jpg, svg, webp).
  • Keep the declaration file under a path that tsconfig includes.
  • Add the image mapping to the Jest config when the first component test is written.
  • Import assets instead of building their paths as strings, so the bundler can fingerprint them.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.