aws-lambda Cannot find module

前端 未结 12 687
野性不改
野性不改 2020-12-06 04:20

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 name

相关标签:
12条回答
  • 2020-12-06 05:06

    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
    
    0 讨论(0)
  • 2020-12-06 05:10

    AWS Lambda uses the name of the file and the name of the handler function, so if you defined your handler like this: exports.myHandler = function(event, context) in a file named index.js, your handler is index.myHandler.

    0 讨论(0)
  • 2020-12-06 05:11

    File Name:
    app.js
    Lambda Function in "app.js":
    exports.handler = function(event, context)...
    Lambda Handler on Amazon Console:
    app.handler ({app}.js + exports.{handler} = app.handler)

    When you unzip the folder, you should see:
    app.js
    node_modules

    0 讨论(0)
  • 2020-12-06 05:13

    I had this problem on a custom module I had built that was in the node_modules dir. Everything ran fine in testing on my Win10 machine, but when uploaded I kept getting that same "Cannot find module 'modulename'" error.

    It turns out that I had a mismatch; here's the package.json line from the module that couldn't be found:

    "main": "./build/modulename.js",
    

    and here's the actual filename:

    Modulename.js
    

    Case-sensitive; Windows isn't, linux (and thus AWS) is.

    0 讨论(0)
  • 2020-12-06 05:14

    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:

    0 讨论(0)
  • 2020-12-06 05:15

    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 enaugh unfortunately by AWS and it caused a lot of headaches for many.

    0 讨论(0)
提交回复
热议问题