21

I just created a Next.js project using npx create-next-app.

Today, I opened my Next.js projects in VS Code and all of the .css imports suddenly start showing this error:

Cannot find module or type declarations for side-effect import of './globals.css' ts(2882)

This happens even on newly created Next.js projects.

I did not update any code or dependencies manually before this issue started. I also tried downgrading TypeScript, but the error persists.

Why is this error suddenly happening, and how can I fix it?

11
  • 6
    More debugging details are needed, such as how you installed the project and how you added TailwindCSS. What is "reopening"? Commented Sep 26 at 11:37
  • 9
    This error means TypeScript can't find type declarations for your CSS import. By default, TypeScript doesn't know how to handle non-code files like CSS. Solution: Add a CSS module declaration so TypeScript understands CSS imports. Create a file named globals.d.ts (or css.d.ts) in your src or project root: Add: declare module '*.css'; Explanation: This tells TypeScript that any import ending in .css is a valid module. It won't check the contents, but it will stop the error. Commented Sep 27 at 11:56
  • 4
    @MontaserIsmail yeah I know that fix the problem. The thing here is that, we did not need to do that, like 1 or 2 weeks ago. So why do we need to add a *.d.ts now? What changed and we do not know? Commented Sep 27 at 18:17
  • 6
    Your VS-Code probably switch the Typescript version for a different version of your current workspace. Click in any ts file -> look at the bottom of the vs-code -> click in Typescript -> then select the same version that is in your package.json. @Magikk @user:20143463 Commented Sep 28 at 10:18
  • 27
    Open any .tsx file. Press Ctrl + Shift + p and type: TypeScript: Select TypeScript version After that, select workspace version. It will search your node_modules for your lib and select it. The error will disappear. Commented Sep 29 at 23:47

2 Answers 2

25

Open any .tsx file. Press Ctrl + Shift + p and type: TypeScript: Select TypeScript version After that, select workspace version. It will search your node_modules for your lib and select it. The error will disappear.

- Guilherme on Sep 29 at 23:47

Sign up to request clarification or add additional context in comments.

Comments

2

Create a css.d.ts or global.d.ts and add this as it's content.

This tells typescript that any import ending with .css is a valid module.

declare module "*.css"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.