aws-lambda Cannot find module
Solution 1
The way I was able to get this to work was:
- Name the file exports.js
- Name the handler, within the file, exports.handler
- Set the handler in the lambda config to exports.handler
- 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:
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
Admin
Updated on July 09, 2022Comments
-
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 over 7 yearsThe 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 withexports.handler
inside them. For Handler inside Lamdba's console, I specify,scripts/transforms.handler
. -
Ocelot20 about 7 yearsI 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 about 7 yearsScratch that..
compress:false
wasn't working, butgulp-zip
was still the culprit. Manually zipping the same files worked. -
mikey over 4 yearsThis is a heck of a gotcha. Thank you very much.
-
Will over 4 yearsOne of the comments on that bug report has instructions for using the command line version
7zip
instead ofCompress-Archive
. This worked for me -
Naidan about 4 yearsChampion. 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 about 3 yearsThis 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 about 2 yearsif I need to use typescript, what else changes I need to do to use index.ts instead of index.js