简体   繁体   中英

Something went wrong installing the \"sharp\" on aws Lambda

I am trying to upload function with an image cropper dependency as an aws lambda. It works perfectly on my local machine( node v14.17.3 ), but fails with the following error when triggering it as a 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)"
"  

The js file name is index.js and I have zipped all the files below. All the file permissions are as follows:

-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

I am not sure how to debug this issue as per the suggestions suggested by the error as the image cropper works fine on my local machine. The image cropper version is:

"node_modules/sharp": {
  "version": "0.29.3",
  "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.29.3.tgz",
....

Any suggestions where am I going wrong?

To fix it on MAC, try this solution:

npm install --platform=linux --arch=x64 sharp

It worked for me.

I would recommend using a layer, much easier to configure and deploy. On the AWS console, you can deploy this layer - 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

If that URL doesn't work, navigate to functions on the console -> Create Function -> Browse Serveless App Repo -> Search for "Sharp", select "nodejs-sharp-lambda-layer".

Deploy the layer -> On your lambda function, select the layer.

I currently have two layers, one for all my other node_modules (axios, etc.) 在此处输入图像描述

It's looking for something in /build/Release/sharp-linux-x64.node . This indicates that this is a binary module with different releases for different operating systems. You're probably building and including a Mac or Windows version of the binary in your deployment, which won't work because Lambda functions run on Amazon Linux.

Your options are:

  • Build your Lambda function on Amazon Linux so it includes the correct binary dependencies. See the Native Modules section ofhere .
  • Find or build a Lambda Layer with the Sharp dependency, and use that in your Lambda function. A quick search turned up a couple projects on GitHub where people have already done this.
  • Switch to a Dockercontainer Lambda deployment . You would use the base NodeJS container image provided by AWS, and any dependencies you installed in that image would automatically be the correct Linux binary.

The issue will occur if any of the packages have a dependency on the Linux platform. In order to solve the issue, use the command below first which will remove the existing Sharp Package-

rm -rf node_modules/sharp

After that, Install the version of Sharp for the Linux platform using the command mentioned below-

npm install --arch=x64 --platform=linux --target=16x sharp

I hope it will help.

Copying my answer here from another question..

The Sharp dependencies built in Linux and Windows environments are different. Try building & creating the zip file in a linux environment. That should fix your issue.

适用于 Sharp 的 Windows 和 Linux node_modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM