tintin
tintin

Reputation: 5867

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?

Upvotes: 16

Views: 11492

Answers (8)

Renan Bronchart
Renan Bronchart

Reputation: 982

I found an other solution works weel for me.

  1. Install latest nodejs@20 version (Change by the same than lambda node version)

https://nodejs.org/en/download Make sure you have the right path to node in you .zshrc or .bash_profile export PATH=$HOME/bin:/usr/local/bin:$PATH

  1. remove package-lock.json

  2. remove node_modules

  3. remove dist folder in your lambda function folder

  4. remove cache: npm cache clean --force

  5. Add pre-install in scripts in package.json and replace target by lambda version of node and same architecture than lambda architecture.

"preinstall": "npm install --platform=linux --arch=x64 --target=20x [email protected]"

  1. Install and force sharp with linux configuration (like lambda configuration)

npm install --platform=linux --arch=x64 --target=20x [email protected] --force

Upvotes: 0

Eduardo Toural
Eduardo Toural

Reputation: 79

Double check the Architecture you are using on AWS side:

Lambda\Functions\YOUR FUNCTION NAME\Edit runtime settings

enter image description here

If you are compiling from Mac M1, pass the Architecture parameter if selected "x86_64" architecture, i.e.

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

Upvotes: 0

MUHAMMAD AZEEM
MUHAMMAD AZEEM

Reputation: 125

if you are using Windows and want to upload a layer to lambda for using sharp please use this command because I got the same error and got fixed by installing sharp with this command. npm install --arch=x64 --platform=linux --target=18x sharp

Upvotes: 2

hahuaz
hahuaz

Reputation: 373

Sharp has distinct builds tailored for each operating system. When developing on a Windows machine, Sharp will generate a build that is specific to the Windows OS. However, since Lambda runs on a Linux OS, you must create a build that is compatible with it.

If you force Docker bundling you are good to go.

bundling: {
  nodeModules: ['sharp'], 
  forceDockerBundling: true, // force docker bundling for sharp
},

Upvotes: 0

Ravindu Fernando
Ravindu Fernando

Reputation: 119

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.

Windows & Linux node_modules for Sharp

Upvotes: 0

DetectiveFalcon
DetectiveFalcon

Reputation: 79

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.) enter image description here

Upvotes: 5

Prosenjit Barman
Prosenjit Barman

Reputation: 81

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.

Upvotes: 4

NineCattoRules
NineCattoRules

Reputation: 2428

To fix it on MAC, try this solution:

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

It worked for me.

Upvotes: 7

Related Questions