我在使用 eslint 检查 typescript 代码时,遇到了一个问题,就是无法解析路径到模块(Unable to resolve path to module):

1
import app from './app';

报错:

1
2:17  error  Unable to resolve path to module './app'  import/no-unresolved

这个问题的解决步骤如下:

1、设置 parserOptions:

1
2
3
4
5
6
7
// .eslintrc.json
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
}

2、设置 import/resolver

1
2
3
4
5
6
7
8
9
10
// .eslintrc.json
{
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
}