user12541086
user12541086

Reputation:

AWS Lambda Nodejs environment inconsistent require

I've created two lambda functions with similar settings a couple days apart. One was able to const qs = require('qs');. The second function was created a couple days later. However it gave the following error when it tried to require qs.

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'qs'\nRequire stack:\n- /var/task/handler.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'qs'",
        "Require stack:",
        "- /var/task/handler.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:956:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)",
        "    at Module.load (internal/modules/cjs/loader.js:812:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:724:14)",
        "    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)",
        "    at internal/main/run_main_module.js:17:11"
    ]
}

Both functions runtime are set to Node.js 12.x. Are lambda environment suppose to be inconsistent like this? Should I just install qs on all my function deployment to make them consistent and avoid errors in the future?

Upvotes: 2

Views: 1094

Answers (2)

Anja Ishmukhametova
Anja Ishmukhametova

Reputation: 1564

also had the same issue.. seem you need add direct dependecies (and not devDependecies) to package.json. Fixed problem.

Upvotes: 0

matsev
matsev

Reputation: 33789

The AWS Lambda runtime only contains the runtime that it has been configured with, e.g. Node.js 12.x in your example. With the exception of the AWS SDK, all third party dependency needs to be packaged together with your application code in a deployment package.

Tools such as AWS SAM (Serverless Application Model) can help with this (e.g. sam package when using AWS SAM).

Upvotes: 2

Related Questions