aws-lambda Cannot find module

69,160

Solution 1

The way I was able to get this to work was:

  1. Name the file exports.js
  2. Name the handler, within the file, exports.handler
  3. Set the handler in the lambda config to exports.handler
  4. Zip up only the contents of the folder, not the folder itself (as mentioned above) and rename the zip file exports.zip

Solution 2

Ok, I did this myself, just make sure that you make the zip such that the .js file doesn't end up inside a folder, because AWS would unzip the file you upload and tries to find a .js file by the name of handler you gave, and if its inside a folder it won't help you.

Solution 3

One possible problem is if you upload the lambda as a zip file created via PowerShell Compress-Archive. Compress-Archive has a bug which causes AWS to extract the files into a flat tree (no subdirectories), with backslashes in filenames:

enter image description here

Solution 4

This exact error can show up if your zipped file(s) do not have world-wide read permission. (chmod -R ugo+r).

Check the file permissions before they are zipped. This is not emphasized enough unfortunately by AWS and it caused a lot of headaches for many.

Solution 5

If you are using AWS Lambda Layers you need to validate if your directory structure is on the needed structure for a layer:

For example for the moment.js node.js module you need the following structure:

aws-lambda-layer.zip
│ nodejs
│ nodejs/node_modules
└ nodejs/node_modules/moment

So to create a layer zip file with the correct structure we can use the following command on the root of our project:

mkdir -p nodejs && cp -r node_modules nodejs/ && zip -r aws-lambda-layer.zip nodejs
Share:
69,160
Admin
Author by

Admin

Updated on July 09, 2022

Comments

  • Admin
    Admin almost 2 years

    I keep getting this error in the aws-lambda console when uploading code from a zip file. I have tried uploading other zip files and they work correctly. The .js file is named "CreateThumbnail.js" in the zip file. I believe the handler is also named properly "CreateThumbnail.handler". the node_modules subdirectory is also setup. Anyone have any idea?

    {
      "errorMessage": "Cannot find module 'CreateThumbnail'",
      "errorType": "Error",
      "stackTrace": [
      "Function.Module._resolveFilename (module.js:338:15)",
      "Function.Module._load (module.js:280:25)",
      "Module.require (module.js:364:17)",
      "require (module.js:380:17)"
      ]
    }
    
  • rodrigo-silveira
    rodrigo-silveira over 7 years
    The actual name of the file is not really important. The key is to fully qualify the file and the path leading to it. My zip file has a directory named scripts, and inside it there are several files with exports.handler inside them. For Handler inside Lamdba's console, I specify, scripts/transforms.handler.
  • Ocelot20
    Ocelot20 about 7 years
    I was using gulp to build and produce my zip file using the gulp-zip package, which produced this form of the issue. Only seemed to happen when it tried to compress it (the default), so using this option made it work for me: .pipe(zip('myfile.zip', { compress: false })). Thanks, I was pulling my hair out on this one for a while.
  • Ocelot20
    Ocelot20 about 7 years
    Scratch that..compress:false wasn't working, but gulp-zip was still the culprit. Manually zipping the same files worked.
  • mikey
    mikey over 4 years
    This is a heck of a gotcha. Thank you very much.
  • Will
    Will over 4 years
    One of the comments on that bug report has instructions for using the command line version 7zip instead of Compress-Archive. This worked for me
  • Naidan
    Naidan about 4 years
    Champion. So easy to trip over this if using OS X "Compress" that puts everything in a folder (as it should).
  • Long M K Nguyễn
    Long M K Nguyễn about 3 years
    This saved me, as i tried to export the current code and it put everything into a folder when exporting so i thought i needed a folder when uploading as well
  • Waleed Ahmad
    Waleed Ahmad about 2 years
    if I need to use typescript, what else changes I need to do to use index.ts instead of index.js