在 aws Lambda 上安装“sharp”时出现问题

tin*_*tin 16 aws-lambda

我正在尝试使用图像裁剪器依赖项作为 aws lambda 上传函数。它在我的本地机器()上完美运行node v14.17.3,但在将其作为 lambda fn 触发时失败并出现以下错误。

"errorType":
"Error"
"errorMessage":
"\nSomething went wrong installing the \"sharp\" module\n\nCannot find module '../build/Release/sharp-linux-x64.node'\nRequire stack:\n- /var/task/node_modules/sharp/lib/sharp.js\n- /var/task/node_modules/sharp/lib/constructor.js\n- /var/task/node_modules/sharp/lib/index.js\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js\n\nPossible solutions:\n- Install with the --verbose flag and look for errors: \"npm install --ignore-scripts=false --verbose sharp\"\n- Install for the current runtime: \"npm install --platform=linux --arch=x64 sharp\"\n- Consult the installation documentation: https://sharp.pixelplumbing.com/install"
"stack":
"Error: "
"Something went wrong installing the \"sharp\" module"
""
"Cannot find module '../build/Release/sharp-linux-x64.node'"
"Require stack:"
"- /var/task/node_modules/sharp/lib/sharp.js"
"- /var/task/node_modules/sharp/lib/constructor.js"
"- /var/task/node_modules/sharp/lib/index.js"
"- /var/task/index.js"
"- /var/runtime/UserFunction.js"
"- /var/runtime/index.js"
""
"Possible solutions:"
"- Install with the --verbose flag and look for errors: \"npm install --ignore-scripts=false --verbose sharp\""
"- Install for the current runtime: \"npm install --platform=linux --arch=x64 sharp\""
"- Consult the installation documentation: https://sharp.pixelplumbing.com/install"
"    at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:30:9)"
"    at Module._compile (internal/modules/cjs/loader.js:1085:14)"
"  
Run Code Online (Sandbox Code Playgroud)

js文件名是index.js,我已经压缩了下面的所有文件。所有文件权限如下:

-rw-r--r--   1 *****  staff     1379 Dec 26 12:22 index.js
-rw-r--r--   1 *****  staff      280 Dec 26 12:59 package.json
drwxr-xr-x   6 *****  staff      192 Dec 26 13:08 ..
-rw-r--r--   1 *****  staff    44154 Dec 26 13:31 package-lock.json
drwxr-xr-x  69 *****  staff     2208 Dec 26 13:31 node_modules
Run Code Online (Sandbox Code Playgroud)

我不确定如何根据错误建议的建议调试此问题,因为图像裁剪器在我的本地计算机上工作正常。图像裁剪器版本是:

"node_modules/sharp": {
  "version": "0.29.3",
  "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.29.3.tgz",
....
Run Code Online (Sandbox Code Playgroud)

有什么建议我哪里出错了吗?

Nin*_*les 8

要在 MAC 上修复此问题,请尝试以下解决方案:

npm install --platform=linux --arch=x64 sharp
Run Code Online (Sandbox Code Playgroud)

这对我有用。

  • 使用 Mac 时,您只需“npm install Sharp”,然后“npmrebuild --platform=linux --arch=x64 Sharp”(如文档 https://sharp.pixelplumbing.com/install#cross-platform 中所述) 。我还将重建命令添加到“预部署”脚本中,这样我确信当其他协作者部署时,Linux 二进制文件也会包含在 Lambda 代码中。 (3认同)

小智 5

我建议使用一个层,这样更容易配置和部署。在 AWS 控制台上,您可以部署此层 - https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/create/app?applicationId=arn: aws:serverlessrepo:us-east-1:987481058235:applications/nodejs-sharp-lambda-layer

如果该 URL 不起作用,请导航到控制台上的函数 -> 创建函数 -> 浏览 Serveless App Repo -> 搜索“Sharp”,选择“nodejs-sharp-lambda-layer”。

部署层 -> 在 lambda 函数上,选择层。

我目前有两层,一层用于所有其他节点模块(axios 等) 在此输入图像描述