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

Viewed 1471

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?

2 Answers

To fix it on MAC, try this solution:

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

It worked for me.

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.

Related