aws-lambda Cannot find module

前端 未结 12 686
野性不改
野性不改 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 04:52

    Some library files might not have global Read so lambda will not be able to read to content and build the content.

    Make sure all files in node_modules are readable before packaging:

    chmod -R +r node_modules
    

    Then zip and upload.

    0 讨论(0)
  • 2020-12-06 04:54

    This is unrelated but google brought me here, so:

    AWS will give you an error:

    Unable to import module '<myfile>': Error
    

    What was really happening for me, was that was requiring an unexisting JS file. The error is a bit misleading.

    0 讨论(0)
  • 2020-12-06 04:56

    This turned out to be a simple one for me.

    I was getting, cannot create index. in my case, my main lambda file with the exports.handler in had to be called index.js

    Try calling your main file CreateThumbnail.js

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

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

    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.

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

    The tutorial tells you to include the following items in your zip file:

    CreateThumbnail.js
    /node_modules/gm
    /node_modules/async

    What it fails to consider is that there are dependencies of the two packages (gm, async) that also need to be part of the package.

    So here's what you have to do:

    1. Change directory to node_modules folder in your project folder, and run the command 'npm install gm async'. This will install gm, async and all their dependencies in this folder.
    2. Now package the 'CreateThumbnail.js' file and the complete 'node_modules' folder into a zip file and upload it. It should work now.

    So your complete package should look something like this:

    CreateThumbnail.js
    /node_modules/.bin
    /node_modules/array-parallel
    /node_modules/array-series
    /node_modules/async
    /node_modules/cross-spawn
    /node_modules/debug
    /node_modules/gm
    /node_modules/isexe
    /node_modules/lodash
    /node_modules/lru-cache
    /node_modules/ms
    /node_modules/pseudomap
    /node_modules/which
    /node_modules/yallist
    
    0 讨论(0)
提交回复
热议问题