0

I created a lambda function using the following method.

  1. Inside a new folder I ran

    npm init
    to initialize a new project

  2. Then I added my code to index.js and ran

    npm install mysql --save
    to save mysql package locally.

  3. After this I zipped the content of this new folder and uploaded the zip to lambda functions.

  4. I ran the test using test in the console and got this error.

    { "errorType": "Error", "errorMessage": "Cannot find module 'mysql'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js", "trace": [ "Error: Cannot find module 'mysql'", "Require stack:", "- /var/task/index.js", "- /var/runtime/UserFunction.js", "- /var/runtime/index.js", " at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)", " at Function.Module._load (internal/modules/cjs/loader.js:725:27)", " at Module.require (internal/modules/cjs/loader.js:952:19)", " at require (internal/modules/cjs/helpers.js:88:18)", " at Runtime.exports.handler (/var/task/index.js:4:17)", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }

Dhairya Tripathi
  • 197
  • 2
  • 14

1 Answers1

0

Seems like the trouble is the uploaded zip file doesnt contains the mysql package you installed or the path your code is looking for the dependencies is not correct.

You can verify that by downloading the zip file.

As you did not mention how you are deploying all these resources.

I would suggest use any framework,

  1. Serverless Framework
  2. AWS SAM

which takes care of your packaging your dependencies and you can deploy them with confidence.

Cannot find MySQL in NodeJS using AWS Lambda

samtoddler
  • 8,463
  • 2
  • 26
  • 21