将eslint与typescript一起使用-无法解析模块的路径 [英] Using eslint with typescript - Unable to resolve path to module
问题描述
我的文件 app.spec.ts 中有此导入:
import app from './app';
哪个会导致此打字稿错误
Which causes this Typescript error
2:17 error Unable to resolve path to module './app' import/no-unresolved
./ app.ts 确实存在,但是我没有将.ts文件编译成.js文件。一旦将.ts文件编译为.js,错误就会消失。
./app.ts does exist, but I have not compiled the .ts file into a .js file. As soon as I compile the .ts file to a .js, the error goes away.
但是,由于eslint应该使用typescript,因此应该使用以下命令解析模块.ts而不是.js。
However, since eslint is supposed to work with typescript, it should resolve modules with the .ts and not the .js.
我还在我的 eslint
配置文件中添加了打字稿信息:
I've also added the typescript information in my eslint
config file:
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
如何配置eslint尝试以.ts而不是.js解析模块?
How can I config eslint in such a way that it tries to resolve modules with the .ts and not the .js?
干杯!
编辑#1
app.ts
的内容:
import bodyParser from 'body-parser';
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { buildSchema } from 'graphql';
const app = express();
const schema = buildSchema(`
type Query {
hello: String
}
`);
const root = { hello: () => 'Hello world!' };
app.use(bodyParser());
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true,
}));
export default app;
推荐答案
您可以通过添加以下内容来设置ESLint模块的导入分辨率您的 .eslintrc.json
配置文件的摘要:
You can set the ESLint module import resolution by adding this snippet to your .eslintrc.json
configuration file:
{
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
...
}
有关解析程序的更多信息: https:/ /github.com/benmosher/eslint-plugin-import#resolvers 。
More informations about resolvers: https://github.com/benmosher/eslint-plugin-import#resolvers.
这篇关于将eslint与typescript一起使用-无法解析模块的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!