2

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?

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

5 Comments

@Link.sc You can also use Lambda Layers to share common packages between multiple Lambda functions. Be smart about it though as you can only use 5 layers in a given function.
But what was strange was that my first function didn't require uploading third party dependencies and it was able to reference qs. Based on your answer I should always install third part packages.
@hvaughan3 Yes, quite right. Bottom line is that you somehow need to provide any third party dependency to your Lambda somehow (c.f. similarly to a service deployed on a web server)
@Link.sc Few suggestions: double check what is actually deployed (e.g. select your first Lambda in the Lambda console, click Actions -> export and then Download deployment package). Alternatively, maybe the function uses qs conditionally, i.e. it can return successfully without referencing qs?
@matsev I looked at both the function in the editor. The require is at the top of both functions and qs was used to format the request in both function.
0

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

Comments