aws-samples / image-optimization

Simple, performant and cost efficient solution for optimizing images using Amazon CloudFront, Amazon S3 and AWS Lambda
MIT No Attribution
171 stars 106 forks source link

Couldn't load sharp #35

Closed tusharr closed 2 weeks ago

tusharr commented 3 months ago

I am trying to run this example and running into issues.

Developer Environment - Mac M1 Deploying to AWS Lambda Node 20.x

When I try to fetch an optimized Image with a URL like https:// YOURDISTRIBUTION.cloudfront.net/images/rio/1.jpeg?format=auto&width=300, it returns an error

Cloudwatch shows the following error:

Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n yarn add sharp --ignore-engines\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install

After many attempts to fix this, I can't get the example to run. Any help is appreciated

achrafsouk commented 3 months ago

Hello, thanks for sharing this. I had the same recently, and this is most likely a package versioning issue, that we should be able to fix. Until we do, the easiest way to overcome this issue is actually to build and deploy the solution from a linux-x64 platform, such an EC2 instance, or CloudShell.

piotrekwitkowski commented 3 months ago

The installation of platform-specific Sharp dependencies for Linux x64 architecture is handled in the prebuild script (as defined in package.json: https://github.com/aws-samples/image-optimization/blob/main/package.json#L8).

To ensure a smooth deployment to x64 Lambda functions, I need your confirmation on a couple of things:

Having these platform-specific dependencies in place will ensure successful deployment of your image optimization solution on x64 Lambda. If you could answer these questions, it would be very helpful in understanding the situation and determining if there's a potential bug!

You can manually install sharp and said dependencies too:

fjanicki commented 3 months ago

I had the same issue as @tusharr and manually installing the sharp binaries like you mentioned have worked for me as a workaround.

ClementCardonnel commented 1 month ago

https://github.com/aws-samples/image-optimization/issues/35#issuecomment-2026313394

I had the same issue and your answer helped me get the solution to work. Thank you!

navamgupta commented 1 week ago

Thank you @piotrekwitkowski after spending a whole day on trying to make this sample work you mentioning the exact packages needed really helped.

For future folks running into (pasting so that the error is searchable) "Something 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"

Just run build and then follow the steps mentioned by @piotrekwitkowski 's post. Alternate would be change the prebuild command to be the one thats mentioned above :)

piotrekwitkowski commented 1 week ago

@navamgupta thanks, glad it helped! Could you tell me what OS and CPU architecture you're running?

navamgupta commented 1 week ago

@piotrekwitkowski Sure, its an older macbook Big sur OS, intel core i7 x86-64 The problem was that the files generated with the command mentioned in the prebuild were darwin and not linux for reasons not yet clear to me, using the command your mentioned explicitly installed the right packages.