Typescript/ESLint error: Unable to resolve path to module 'aws-lambda' import/no-unresolved

Viewed 7977

I have a serverless project written with typescript. When I run ESLint I am getting the following error:

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

For the following line of code: import { APIGatewayProxyEvent, APIGatewayProxyHandler, APIGatewayProxyResult } from 'aws-lambda'; (I have "@types/aws-lambda": "^8.10.64" in my devDependencies).

I don't understand what could be the error, as the typing is installed and if I try to import from "@types/aws-lambda" VS Code tells me to import from "aws-lambda" directly.

What am i doing wrong here please?

3 Answers

hendrixchord's anwser helped me to resolve the issue, but here is the trimmed down version of the necessary steps that really worked for me:

# 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

In the .eslintrc file (note that I'm using the .js version):

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

This is the config in my .eslintrc file

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

Also I've noticed in your tsconfig.json file you've not included typeRoots since you've excluded node_modules in your exclude property. It should be like this

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

If all of that fails the last resort would be to add it to the ignore list in .eslintrc file.

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

If I use AWSLambda.APIGatewayProxyEvent when I define the type, it works for me:

const event: AWSLambda.APIGatewayProxyEvent = ...

I didn't have to modify anything.

Related