Nest.js项目ESLint解析错误:TSConfig未包含目标文件
解决Nest.js项目中ESLint解析错误(TSConfig未包含目标文件)
问题场景
通过nest new myapp创建全新Nest.js项目,执行以下命令生成product模块、服务与控制器:
nest g module product; nest g service product; nest g controller product;
之后所有文件出现ESLint解析错误,提示信息如下:
ESLint被配置为基于
parserOptions.project指定的/tsconfig.json对~/tutorials/nodejs/typeorm/simple-app/src/photo/photo.entity.ts进行检查,不过该TSConfig未包含此文件。
以下是当前项目的.eslintrc.js和tsconfig.json配置:
原始.eslintrc.js
module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', tsconfigRootDir: __dirname, sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', ], root: true, env: { node: true, jest: true, }, ignorePatterns: ['.eslintrc.js',], rules: { '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', }, };
原始tsconfig.json
{ "compilerOptions": { "module": "commonjs", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, "outDir": "./dist", "baseUrl": "./", "incremental": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false } }
三种解决方案
方法一:修改ESLint配置排除目标文件
在.eslintrc.js的ignorePatterns数组中添加photo.entity.ts的路径,让ESLint跳过对该文件的检查:
module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.json', tsconfigRootDir: __dirname, sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', ], root: true, env: { node: true, jest: true, }, ignorePatterns: ['.eslintrc.js', 'src/photo/photo.entity.ts'], // 新增排除路径 rules: { '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', }, };
方法二:修改TSConfig纳入目标文件
在tsconfig.json中添加include字段,将src目录下所有TypeScript文件纳入编译范围,这样ESLint就能通过TSConfig找到该文件:
{ "compilerOptions": { "module": "commonjs", "declaration": true, "removeComments": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "es2017", "sourceMap": true, "outDir": "./dist", "baseUrl": "./", "incremental": true, "skipLibCheck": true, "strictNullChecks": false, "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, "noFallthroughCasesInSwitch": false }, "include": ["src/**/*"] // 新增include字段,包含src下所有文件 }
方法三:创建新TSConfig并在ESLint中引用
- 在项目根目录创建
tsconfig.eslint.json,配置如下:
{ "extends": "./tsconfig.json", "include": [ "src/**/*", "src/photo/photo.entity.ts" // 明确包含目标文件 ] }
- 修改
.eslintrc.js的parserOptions.project指向新的TSConfig文件:
module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: 'tsconfig.eslint.json', // 修改为新的配置文件 tsconfigRootDir: __dirname, sourceType: 'module', }, plugins: ['@typescript-eslint/eslint-plugin'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended', ], root: true, env: { node: true, jest: true, }, ignorePatterns: ['.eslintrc.js',], rules: { '@typescript-eslint/interface-name-prefix': 'off', '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/explicit-module-boundary-types': 'off', '@typescript-eslint/no-explicit-any': 'off', }, };
内容的提问来源于stack exchange,提问作者danilo
相关产品推荐
相关产品推荐

