问题 运行 AWS Lambda 函数与 Sharp 库

Problem running AWS Lambda function with Sharp library

我按照 AWS 研讨会 (https://amplify-workshop.go-aws.com/70_generating_thumbnails/10_creating_a_photo_processor_lambda.html) 通过 aws-amplify CLI 创建了一个 lambda 函数。似乎 Node.js 某处存在冲突版本问题。

我认为是 sharp 库的问题,所以我尝试将版本更改为最新版本以查看是否可以解决问题,但并没有解决问题。

CloudWatch 错误日志:

module initialization error: Error
was compiled against a different Node.js version using
NODE_MODULE_VERSION 67. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at Object.Module._extensions..node (module.js:681:18)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:10:15)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)

我相信有一个简单的解决方法,但我不确定问题的根源在哪里。谢谢。

看起来您使用 Node.js 的 lambda 不兼容版本构建了 node_modules。仅限 Lambda supports Node.js 6.10 和 8.10。

尝试将本地节点版本更改为 8.10、删除 node_modulesnpm install、包含 node_modules 的捆绑文件夹并上传到 lambda。这应该有效。

安装 sharp 时使用您的 aws lambda 节点版本。

rm -rf node_modules/sharp
npm install --arch=x64 --platform=linux --target=10.4.1 sharp

以下对我有用:

npm_config_arch=x64 npm_config_platform=linux npm install sharp

由于某种原因,参数无效。