내 파일 app.spec.ts 에이 가져 오기가 있습니다 .
import app from './app';
이 Typescript 오류의 원인
2:17 error Unable to resolve path to module './app' import/no-unresolved
./app.ts 가 존재하지만 .ts 파일을 .js 파일로 컴파일하지 않았습니다. .ts 파일을 .js로 컴파일하자마자 오류가 사라집니다.
그러나 eslint는 typescript와 함께 작동해야하므로 .js가 아닌 .ts로 모듈을 확인해야합니다.
또한 eslint
구성 파일 에 typescript 정보를 추가했습니다 .
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "./tsconfig.json"
}
.js가 아닌 .ts로 모듈을 해결하려고 시도하는 방식으로 eslint를 구성하려면 어떻게해야합니까?
건배!
# 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;
답변
다음 스 니펫을 .eslintrc.json
구성 파일 에 추가하여 ESLint 모듈 가져 오기 분석을 설정할 수 있습니다 .
{
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
...
}
리졸버에 대한 추가 정보 : https://github.com/benmosher/eslint-plugin-import#resolvers .
답변
이것은 나를 위해 그것을합니다.
.eslintrc.js
{
...
settings: {
...
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx'],
moduleDirectory: ['node_modules', 'src/'],
},
},
}
}
답변
나는 같은 문제가 있었고 나는 그것을 사용하여 해결할 수 있었다.
"plugin:import/errors",
"plugin:import/warnings",
"plugin:import/typescript",
여기에 설명 된대로 .eslintrc.js의 “확장”아래에 eslint-plugin-import
답변
“eslint”를 사용 하는 경우 “typescript” 와 함께 “6.8.0 “: “3.8.3” 에 다음을 추가하는 것 외에 .eslintrc
:
"settings": {
"import/resolver": {
"node": {
"paths": ["src"],
"extensions": [".js", ".jsx", ".ts", ".tsx"],
}
},
}
이 줄을 tsconfig.json
아래 에 추가해야합니다 compilerOptions
.
"compilerOptions": {
...
// Base directory to resolve non-relative module names.
"baseUrl": "src",
...
}
답변
이것은 나를 위해 일한 유일한 솔루션이었습니다.
먼저이 패키지를 설치해야합니다.
yarn add -D eslint-import-resolver-typescript
그런 다음이를 .eslintrc
파일에 추가 하여 별칭 설정 tsconfig.json
을 ESLint로 로드 할 수 있습니다 .
{
"settings": {
"import/resolver": {
"typescript": {}
},
},
}
답변
사람들을 위해 사용하는 사람 babel-module
단지를 추가 extensions
가이 같은 것 때문에 :
"babel-module": {
"extensions": [".js", ".jsx", ".ts", ".tsx"],
"alias": {
"app": "./app"
}
}
답변
ts 와 eslint 가 모두 나에게 짖어 대고 있었기 때문에 .eslintrc 파일에 다음을 추가해야했습니다 .
{
...
"rules": {
....
"import/extensions": "off"
},
"settings": {
....
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
