【问题标题】:Typescript/ESLint error: Unable to resolve path to module 'aws-lambda' import/no-unresolvedTypescript/ESLint 错误:无法解析模块“aws-lambda”导入/未解析的路径
【发布时间】:2021-03-29 20:11:55
【问题描述】:

我有一个用 typescript 编写的无服务器项目。当我运行 ESLint 时,出现以下错误:

  1:85  error  Unable to resolve path to module 'aws-lambda'  import/no-unresolved

对于以下代码行:import { APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda';(我的 devDependencies 中有 "@types/aws-lambda": "^8.10.64")。

我不明白可能是什么错误,因为安装了打字,如果我尝试从“@types/aws-lambda”导入,VS Code 告诉我直接从“aws-lambda”导入。

请问我在这里做错了什么?

【问题讨论】:

标签: typescript eslint typescript-typings typescript-eslint


【解决方案1】:

hendrixchord 的 anwser 帮助我解决了这个问题,但这里是对我真正有用的必要步骤的精简版:

# It's a must, otherwise the below `settings` won't work:
$ npm install --save-dev eslint-import-resolver-typescript
# Not `--save-dev`.
$ npm install @types/aws-lambda

.eslintrc 文件中(注意我使用的是.js 版本):

  settings: {
    'import/resolver': {
      typescript: {
        alwaysTryTypes: true,
      },
    },
  },

【讨论】:

    【解决方案2】:

    如果我在定义类型时使用AWSLambda.APIGatewayProxyEvent,它对我有用:

    const event: AWSLambda.APIGatewayProxyEvent = ...
    

    我不需要修改任何东西。

    【讨论】:

      【解决方案3】:

      这是我的 .eslintrc 文件中的配置

         "settings": {
              "import/parsers": {
                  "@typescript-eslint/parser": [".ts", ".tsx"]
              },
              "import/resolver": {
                  "typescript": {
                      "alwaysTryTypes": true,
                      "paths": "./tsconfig.json",
                  },
              }
          },
      

      我还注意到,在您的 tsconfig.json 文件中,您没有包含 typeRoots,因为您在 exclude 属性中排除了 node_modules。应该是这样的

          "typeRoots": [
              "./node_modules/@types"
          ],
          "exclude": [
              "/node_modules/",
              ".serverless/**/*",
              ".webpack/**/*",
              "_warmup/**/*",
              "vscode/**/*",
              "lib/**/*",
              "tests"
          ]
      

      如果所有这些都失败了,最后的办法是将其添加到 .eslintrc 文件中的忽略列表中。

          "import/no-unresolved": [
              "error",
              {
                  "ignore": [
                      "aws-lambda"
                  ]
              }
          ],
      

      【讨论】:

        猜你喜欢
        • 2020-05-06
        • 2023-01-24
        • 2021-11-10
        • 2022-01-15
        • 2017-05-26
        • 2019-06-30
        • 2022-12-19
        • 2018-10-08
        • 2021-12-13
        相关资源
        最近更新 更多