4

I'm trying to build a project and I just moved my code from VSCode to Codesandbox. I must mention that the code was working fine in VSC but for some reason it's not working in Codesandbox - the CSS modules can't be found. I am thinking I am maybe missing something in my setup since I looked at other examples and the code seems identical (the imports). The error in getting for all the files is:

Cannot find module './Example.module.css' or its corresponding type declarations.

The complete code is here:

https://codesandbox.io/s/burger-builder-project-173uw?file=/src/Components/Layout/Layout.tsx

Does anyone have any idea what could be wrong? Thank you.

doğukan
  • 23,073
  • 13
  • 57
  • 69
  • Does this answer your question? [Can't import CSS/SCSS modules. TypeScript says "Cannot Find Module"](https://stackoverflow.com/questions/40382842/cant-import-css-scss-modules-typescript-says-cannot-find-module) – Akber Iqbal Aug 09 '20 at 11:39

4 Answers4

3

To get rid of the error you need to create a Typescript Declaration file for *.css on the root folder and include it on the tsconfig.json. Take a look on the changes below:

module.css.d.ts

declare module "*.module.css";

tsconfig.json

{
    "include": [
        "./src/*",
        "./module.css.d.ts"
    ],
    "compilerOptions": {
        "strict": true,
        "esModuleInterop": true,
        "lib": [
            "dom",
            "es2015"
        ],
        "jsx": "react"
    }
}
lepsch
  • 8,927
  • 5
  • 24
  • 44
0

I found one way to fix this (that's how I work with CSS files):

// Layout.tsx

import React from "react";
import BurgerBuilder from "../../ContainerBurgerBuilder";

// CSS
import "./Layout.module.css";

And then in your render method:

render() {
   return (
      <div className="Content">
         <BurgerBuilder/>
      </div>

   );
}
wgumenyuk
  • 420
  • 5
  • 16
  • My code, surprisingly, seems to be working, I just can't get rid of the error. Your solution breaks the whole app. Thank you. –  Aug 09 '20 at 11:52
  • I am sorry to hear that. I hope you will find a more suitable solution soon. – wgumenyuk Aug 09 '20 at 12:04
0

enable javascript in your html file, then i think it will work

0

add "Typescript Plugin Css Modules" from your dependency list in codesandbox.io

enter image description here

enter image description here

blackgreen
  • 34,072
  • 23
  • 111
  • 129