AWS Lambda: Unable to import module

后端 未结 6 1149
误落风尘
误落风尘 2021-02-05 13:12

please forgive me, I am totally new at Lambda and Node.

I am trying to replicate this git to order a pizza using an AWS IoT button.

My current code is:

<

相关标签:
6条回答
  • 2021-02-05 13:21

    I had the same issue and got it solved by the following the below steps

    1. dont use the default zip option provided in the finder in mac. use terminal to zip

    cd foldername

    zip -r foldername.zip *

    1. use exports in all your js functions which you want to use in the index.js file.

    Say in Javascript file a.js

    var func = function(){
    
    }
    
    export.func = func ; 
    

    In index.js

    var a = require('a.js')
    exports.handler(event, context, callback){
    
    a.func
    
    }
    
    0 讨论(0)
  • 2021-02-05 13:30

    What worked for me was to zip the following files and upload the zip(after doing npm install in the folder):

    • node_modules/
    • your_file1.js
    • your file2.js
    • your files.js
    • package.json
    • package-lock.json
    0 讨论(0)
  • Ran into this problem as well. What solved it for me was realizing that the file path was too long on a Windows machine. After zipping, I realized that the contents of node_modules was empty. I copied the files for zipping to a higher level path e.g. C:\User\ and zipped the specified files. Hope this helps!

    0 讨论(0)
  • 2021-02-05 13:36

    In our case, it is neither path or permission issue. We got this error because we do npm prune --production before we deploy, and we have some runtime packages that is incorrectly placed under devDependencies which get wiped out during that phase. Unfortunately the lambda only gives a vague error message.

    0 讨论(0)
  • 2021-02-05 13:44

    In my case, i mentioned Handler as index.handler but my root filename is app.js. Changing this to index.js worked.

    Also make sure the zip file has your index.js, node_modules and package.json directly.

    Should be:

    zip file --> index.js
                 package.json
                 node_modules
    

    Not

    zip file --> some_folder_name --> index.js
                                      package.json
                                      node_modules
    
    0 讨论(0)
  • 2021-02-05 13:44

    It was a permission issue for me, after I changed the permissions for the 'node_modules' folder to 777, zipped and uploaded it, it worked.

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