0
votes

Good day! I am testing an AWS Lambda Function that uses an AWS Lambda Layer with the following directory:

LambdaLayer.zip
  nodejs/
    package.json
    package-lock.json
    node_modules/
      jsonwebtoken/

In my Lambda Function (running on node 14.x runtime), I am importing the module from the layer like this:

const jwt = require('jsonwebtoken');

However, I am getting the following error:

ERROR   Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'jsonwebtoken'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'jsonwebtoken'","Require stack:","- /var/task/index.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","

I already followed the directory structure as per AWS documentation and as also stated here: AWS lambda layers error when call API "cannot find module". Still, I'm getting this error.

I also tried the below directory structure but still did not work:

LambdaLayer.zip
  nodejs/
    node14/
      node_modules/
        jsonwebtoken/

Am I missing something?

P.S. I have compressed the directory using zip -r LambdaLayer.zip LambdaLayer/ and also uploaded it to Lambda layer manually via the AWS Console.

1

1 Answers

0
votes

I already found the root cause of the issue. What I was actually doing is that I'm zipping the LambdaLayer folder as the top-level directory instead of nodejs.

What I thought I was doing:

LambdaLayer.zip
  nodejs/
    node_modules/
      jsonwebtoken/

What I am actually doing:

LambdaLayer.zip
  LambdaLayer/
    nodejs/
      node_modules/
        jsonwebtoken/

This is why the function cannot find the module. After zipping the nodejs folder instead of the LambdaLayer, it now worked.