6

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

3 Answers 3

6

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,
      },
    },
  },
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be the relevant issue: github.com/import-js/eslint-plugin-import/issues/1485 Still, I don't understand why @types/aws-lambda should not be placed in devDependencies. Any good reason for that?
4

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"
            ]
        }
    ],

Comments

3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.