tsconfig paths not importing css modules, only js/ts files
Asked Answered
L

0

6

I used Vite to scaffold a typescript-react project, and i wanted to use the given tsconfig.json file to create shortcuts for my directories, so i wont have to deal with long relative paths when importing stuff in my components.

This is tsconfig.json :

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "baseUrl": "./src",
    "paths": {
      "@styles/*": ["styles/*"],
      "@routes/*": ["routes/*"],
      "@components/*": ["components/*"],
      "@assets/*": ["assets/*"]
    }
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

I get an error when trying to import LandingPage.module.css into my component LandingPage.tsx using this line : import styles from "@styles/LandingPage.module.css";

here's the relevant folder structure :

folder structure

The Intellisense in VS Code when i type import styles from "@styles/" shows only the js file in styles folder, and doesn't pick up on the css module.

Attempted solution :

A comment about restarting the Typescript server, didn't work.

Ref : This SO question

Lyingin answered 14/10, 2022 at 17:33 Comment(1)
Did you ever resolve this? I'm also seeing it in a current project, VSCode 1.82.0. Same type of setup, I have a path in my tsconfig.json that looks like: "@/*": ["./src/*"], and my import in a tsx file referencing '@/styles/Home.module.css' (which exists) fails with "Cannot find module..." I've done the similar types of things, restarted the TS language server, etc. and the error persists.Littlest

© 2022 - 2024 — McMap. All rights reserved.