Maaz Zindani

Loading the index…

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

TypeScript cannot find the module for a .scss or CSS module import

ErrorCannot find module './Button.module.scss' or its corresponding type declarations.

Short answer

TypeScript only knows how to read code files, so an import of a stylesheet has no type and it reports the module as missing. The bundler handles the file, but the compiler needs a declaration. Add a wildcard ambient module for the file pattern in a .d.ts file that has no top-level import or export, and make sure tsconfig includes it.

  • Applies to TypeScript 5.9, webpack 5, sass-loader 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check whether any .d.ts file in the project already declares the pattern, and what tsconfig includes.

    grep -rn "declare module" src --include=*.d.ts
    npx tsc --showConfig | grep -A4 include
  2. 2Match what you see, then jump to its fix

    1. 1TS2307 on every import that ends in .scss or .cssNo declaration exists for the file typeFix
    2. 2The declaration exists but the error staysThe declaration file is itself a moduleFix
    3. 3The declaration file is fine but sits outside the folders in includetsconfig does not include the declaration fileFix

What does the error look like?

  • Cannot find module './Button.module.scss' or its corresponding type declarations.
  • TS2307: Cannot find module './styles.module.css' or its corresponding type declarations.

Cause 1 of 3

No declaration exists for the file type

1Check

Search for a declaration. The grep in the first step returns nothing for these patterns.

grep -rn "module.scss" src --include=*.d.ts

2Fix

Add a declaration file with a wildcard pattern. The shape of the default export is your choice; a map from class name to string is common.

// src/types/scss.d.ts
declare module "*.module.scss" {
  const classes: { readonly [key: string]: string };
  export default classes;
}

3Verify

Run the type check.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

The declaration file is itself a module

1Check

A file with a top-level import or export is a module, and then declare module is a module augmentation instead of an ambient declaration. Look at the top of the file.

grep -n "^import\|^export" src/types/scss.d.ts

2Fix

Remove the top-level import and export lines from that file. Inside the declare module block imports are fine.

// src/types/scss.d.ts  (no top-level import or export)
declare module "*.module.scss" {
  const classes: { readonly [key: string]: string };
  export default classes;
}

3Verify

Run the type check.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

tsconfig does not include the declaration file

1Check

List the files the compiler loads and see if the .d.ts file is among them.

npx tsc --listFilesOnly | grep scss.d.ts

2Fix

Add its folder to include, or the file itself.

{
  "include": ["src/**/*", "types/scss.d.ts"]
}

3Verify

Run the type check again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Keep all wildcard declarations in one folder that tsconfig includes.
  • Use the same pattern in the declaration as the file names, such as *.module.scss.
  • Run tsc --noEmit in CI, since the bundler alone will not report type errors.

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.