简体   繁体   English

“找不到模块‘./something.module.scss’或其相应的类型声明

[英]"Cannot find module './something.module.scss' or its corresponding type declarations

I'm trying to use scss modules with react typescript. This feature should be baked into React from react-scripts@2.0.0 and higher as it says on their website .我正在尝试将 scss 模块与 react typescript 一起使用。这个功能应该从 react-scripts@2.0.0 和更高版本融入到 React 中,正如他们网站上所说的那样。

For typescript support and intellisense, I am using the typescript-plugin-css-modules module.对于 typescript 支持和智能感知,我正在使用 typescript -plugin-css-modules模块。

Hovering over css imports for classnames work just fine:将鼠标悬停在 css 导入类名上工作得很好:

将鼠标悬停在 css 导入上,显示智能感知类名

Here's my code:这是我的代码:


src/pages/homePage/homePage.tsx src/pages/homePage/homePage.tsx

import styles from './styles.module.scss';
 
const HomePage = () => {
    return <div className={styles.myStyle}>Home page</div>;
};
    
export default HomePage;

src/pages/homePage/styles.module.scss src/pages/homePage/styles.module.scss

.myStyle {
    color: red;
}

Error message on screen:屏幕上的错误信息:

网站上的错误信息

As you can see, the color is correct in the background, and dismissing the error makes the website work just fine.如您所见,背景颜色是正确的,消除错误会使网站正常运行。 Therefore, it is safe to assume that the error is only made by typescript. I have seen articles about this, like this one where they declare a style.d.ts file in the module root dir, for declaring css modules, with something like this inside:因此,可以安全地假设该错误仅由 typescript 造成。我看过有关此的文章,例如这篇文章,他们在模块根目录中声明了一个 style.d.ts 文件,用于声明 css 模块,其中包含类似这个里面:

// For SCSS
declare module "*.module.scss" {
  const classes: { [key: string]: string };
  export default classes;
}

However, this produces an error as classes is already defined, because this file is already created by react scripts:但是,这会产生一个错误,因为已经定义了类,因为这个文件已经由反应脚本创建:

node_modules/react-scripts/lib/react-app.d.ts node_modules/react-scripts/lib/react-app.d.ts

[...]

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

[...]

So, since react-scripts already create these for me, and I am using the typescript-plugin-css-modules module, everything should be fine.因此,由于 react-scripts 已经为我创建了这些,并且我正在使用typescript-plugin-css-modules模块,所以一切都应该没问题。 I am not receiving any red lines or errors in the code editor, only in the website.我没有在代码编辑器中收到任何红线或错误,仅在网站中收到。

I hope someone has a solution, thanks in advance!我希望有人有解决方案,在此先感谢!

I found the answer.我找到了答案。 I accidentally deleted react-app-env.d.ts after using create-react-app.我在使用 create-react-app 后不小心删除了 react-app-env.d.ts。 This must exist in order to use CSS modules.这必须存在才能使用 CSS 模块。 If your project is also missing this file.如果您的项目也缺少此文件。 Add it to your src folder and write将它添加到您的 src 文件夹并写入

/// <reference types="react-scripts" />

As far as I know, you can use module.scss but u should import style from module.css, not module.scss.据我所知,你可以使用 module.scss 但你应该从 module.css 导入样式,而不是 module.scss。 It works for me.这个对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块“module.css”或其相应的类型声明 - Cannot find module 'module.css' or its corresponding type declarations 找不到模块“graphql-hooks”或其相应的类型声明 - Cannot find module 'graphql-hooks' or its corresponding type declarations 找不到模块“./App.svelte”或其对应的类型声明 - Cannot find module './App.svelte' or its corresponding type declarations 找不到模块“react-lazily”或其相应的类型声明 - Cannot find module 'react-lazily' or its corresponding type declarations codesandbox - 找不到模块 PACKAGE 或其相应的类型声明 - codesandbox - Cannot find module PACKAGE or its corresponding type declarations 如何解决找不到找不到模块'../home/featuredRooms'或其对应的类型声明。? - how to solve cannot find Cannot find module '../home/featuredRooms' or its corresponding type declarations.? VueJS/Typescript - 找不到模块“./components/Navigation”或其相应的类型声明 - VueJS/Typescript - Cannot find module './components/Navigation' or its corresponding type declarations ReScript,TypeScript:找不到模块“@rescript/react/src/ReactDOM.gen”或其相应的类型声明 - ReScript, TypeScript: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations 如何修复找不到模块“./aws-export”或其对应的类型 declarations.ts (2307) - How to fix Cannot find module './aws-export' or its corresponding type declarations.ts (2307) 为什么我收到此错误找不到模块“nestjs/common”或其相应的类型声明 - Why I got this error Cannot find module 'nestjs/common' or its corresponding type declarations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM