TypeScript cannot find module './logo.png' or its type declarations
Cannot 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.
Fix it fast
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.json2Match what you see, then jump to its fix
- 1
TS2307 on a .png, .svg or .jpg import in a Vite projectNo asset types are loaded (Vite)Fix - 2
The 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
Cannot find module './logo.png' from 'src/App.tsx', coming from Jest and not tscJest cannot resolve the image at test timeFix
- 1
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*.json2Fix
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 --noEmitConfirmed by vite.dev
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.json2Fix
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 --noEmitConfirmed by vite.dev
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 moduleNameMapper2Fix
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 jestConfirmed by jestjs.io
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
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 fixWeb apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
Cypress refuses to start with 'Your configFile is invalid'
Your configFile threw an error from: /path/to/cypress.config.ts
Cypress could not load cypress.config.js or .ts, or found something wrong in it.
5 causes, each with a fixWeb apps
ESLint cannot decide which copy of the @typescript-eslint plugin to use
The message lists two file paths for the same plugin, each with the name of the config that loaded it
ESLint found the same plugin at two different file paths and will not pick one.
3 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fix