Lambda代码可在节点v8.10上运行,但不能在nodejs v10.x上运行 [英] Lambda code works on node v8.10 but not nodejs v10.x

查看:61
本文介绍了Lambda代码可在节点v8.10上运行,但不能在nodejs v10.x上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在完成lambda课程,该课程是使用v8.10完成的.我正在尝试使用nodejs v10.x,因为这是我将来在项目中要使用的.

I am working through a lambda course which was done using v8.10. I am trying to use nodejs v10.x since that is what I want to use for my project in the future.

我不明白为什么"const uuid = require('uuid');"该行仅在nodejs v10中引发导入错误,但在v8.10中,代码运行良好.

I am not understanding why the "const uuid = require('uuid');" line is throwing an import error only in nodejs v10 but in v8.10 the code runs just fine.

代码:

const aws = require('aws-sdk');
const s3 = new aws.S3();
const uuid = require('uuid');

exports.handler = async (event) => {
    console.log("Get the event to our S3POC class - " + JSON.stringify(event));

    const newUUID = uuid.v4();

    console.log("The file name is:" + newUUID);

    //put our sentence into the s3 bucket
    return s3.putObject({
        Bucket: "helloworld-s3.arkhadbot.com",
        Key: "test" + ".json"
    });
};

错误

Response:
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'uuid'",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'uuid'",
    "    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:45:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
    "    at Module.load (internal/modules/cjs/loader.js:653:32)",
    "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
    "    at startup (internal/bootstrap/node.js:283:19)"
  ]
}

Request ID:
"e9c300ff-d12e-47b9-a45a-996a1f78b5f5"

Function Logs:
START RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5 Version: $LATEST
2019-09-12T12:18:34.743Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'uuid'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'uuid'","    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:45:30)","    at Module._compile (internal/modules/cjs/loader.js:778:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)","    at Module.load (internal/modules/cjs/loader.js:653:32)","    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)","    at Function.Module._load (internal/modules/cjs/loader.js:585:3)","    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)","    at startup (internal/bootstrap/node.js:283:19)"]}
END RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5
REPORT RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5  Duration: 5098.92 ms    Billed Duration: 5100 ms    Memory Size: 128 MB Max Memory Used: 36 MB  
XRAY TraceId: 1-5d7a3795-41360f9ed871797c8ff32c3e   SegmentId: 4f526b023d16ac66 Sampled: false  
Unknown application error occurred
Runtime.ImportModuleError

推荐答案

由于node.js v10 aws lambda不支持从lambda本身导入库.

Since node.js v10 aws lambda does not support importing libraries from lambda itself.

从文档中:

部署程序包是一个ZIP归档文件,其中包含您的功能代码 和依赖性.如果使用,则需要创建一个部署程序包 Lambda API来管理功能,或者如果您需要包括 库和AWS开发工具包以外的其他依赖关系.

A deployment package is a ZIP archive that contains your function code and dependencies. You need to create a deployment package if you use the Lambda API to manage functions, or if you need to include libraries and dependencies other than the AWS SDK.

如果您的功能 依赖于JavaScript的SDK以外的其他库,请安装它们 到具有NPM的本地目录,并将它们包括在您的部署中 包裹.如果您需要 比运行时中包含的版本更新的版本,或确保 该版本将来不会更改.

If your function depends on libraries other than the SDK for JavaScript, install them to a local directory with NPM, and include them in your deployment package. You can also include the SDK for JavaScript if you need a newer version than the one included on the runtime, or to ensure that the version doesn't change in the future.

有关 AWS Lambda部署程序包中的Node.js

更新02/05/2020:

node.js 8.10现在已被弃用,您应该使用node.js 10或12.

node.js 8.10 is now deprecated, you should use node.js 10 or 12.

https://docs.aws.amazon .com/lambda/latest/dg/runtime-support-policy.html

这篇关于Lambda代码可在节点v8.10上运行,但不能在nodejs v10.x上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆