9

I use VS Code for development of my AWS hosted serverless application. The app uses Lambdas. Recently, I've decided to start using Lambda Layers to extract and reuse common code. The problem that I have is that AWS Lambda expects the following import of Lambda layer:

const layer = require("/opt/layer");

I would like to get Intellisense on layer exported functions:

module.exports = {
    f1(param1, param2) {
        // ...
    },

    f2(paramX, paramY, paramZ) {
        // ...
    }
}

And despite I possess both lambda and lambda layer code, VS Code naturally can't resolve the path to layer file and thus Intellisense doesn't work.

I've found that if I put the next jsconfig.json file anywhere in my project:

{
    "compilerOptions": {
        "target": "ES6",
        "module": "commonjs"
    },
    "exclude": [
        "node_modules",
        "**/node_modules/*"
    ]
}

require statements stop to be shown in red and some basic text autocompletion is allowed. But it doesn't really show layer exported functions with parameters correctly.

I would like not to create solutions like using custom imports during development then substitute them with "require("/opt/layer")" during deployment to AWS (or at least have some automated thing).

What can be done?

Arsenii Fomin
  • 3,120
  • 3
  • 22
  • 42
  • Might be helpful for you: https://stackoverflow.com/a/57553831/2692307 – Lukas Bach Nov 05 '19 at 14:20
  • @LukasBach Thank you for the suggestion but looks like it doesn't affect anything. I've noticed that even presence of "jsconfig.json" file without any content in it removes errors on "require"... some magic here. But Intellisense still doesn't work properly – Arsenii Fomin Nov 05 '19 at 14:33

1 Answers1

8

Finally, the next jsconfig.json file located in lambda folder worked for me (restarting VS Code also required):

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es2015",
        "moduleResolution": "node",
        "baseUrl": ".",
        "paths": {
            "/opt/layer1": ["./layers/layer1"],
            "/opt/layer2": ["./layers/layer2"],
            "/opt/layer3": ["./layers/layer3"]
        }
    },
    "exclude": ["./layers/node_modules"]
}
Arsenii Fomin
  • 3,120
  • 3
  • 22
  • 42
  • 1
    For the folks who try to get IntelliSense on node_modules, be sure to define path specifically: `"paths": {"googleapis": ["../layers/nodejs/node_modules/googleapis"]}` – hahuaz Feb 23 '22 at 05:28