首页 > 解决方案 > 指向文件而不是目录时,Eslint 无法解析模块的路径

问题描述

我有一个带有 Typescript 的 React JS 项目,我使用 Eslint 进行 linting。我已经添加了路径别名tsconfig.jsonwebpack.config.json看起来像这样

// webpack.config.json
components: path.resolve(__dirname, './../src/components/')
...

// tsconfig.json
"components/*": ["components/*"],
...

所以,如果我有一个像import src/components/Button/index.tsxas一样的文件components/Button,Eslint 是安静的。

当我有一个像src/components/Loader.tsx导入为components/Loader. 短绒在这里抱怨Unable to resolve path to module 'components/Loader'

请注意:

我该如何解决这个问题?

标签: reactjswebpackeslint

解决方案


使用 components/Loader 与 components/Loader/index.tsx 相同,但在您的情况下,没有包含 index.tsx 文件的 Loader 文件夹要删除警告,请使用 loader 和 index.tsx 文件创建文件夹结构或明确定义您正在通过 components/Loader.tsx 访问文件


推荐阅读