Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couldn't load sharp #35

Closed
tusharr opened this issue Mar 26, 2024 · 7 comments
Closed

Couldn't load sharp #35

tusharr opened this issue Mar 26, 2024 · 7 comments

Comments

@tusharr
Copy link

tusharr commented Mar 26, 2024

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
Copy link
Contributor

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
Copy link
Contributor

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:

  • Have you run npm run build before executing cdk deploy? This step is crucial to install the necessary dependencies.
  • Can you verify the contents of the /functions/image-processing/node_modules/@img directory in your environment? Specifically, we're looking for the presence of @img/sharp-linux-x64 and @img/sharp-libvips-linux-x64 packages. These are essential for your Lambda function to work correctly on x64 architecture.

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:

  • if present, delete /functions/image-processing/node_modules,
  • if present, delete /functions/image-processing/package.json,
  • if present, delete /functions/image-processing/package-lock.json,
  • run npm install -f --omit=optional --prefix functions/image-processing sharp@^0.33.3 @img/sharp-linux-x64 @img/sharp-libvips-linux-x64

@fjanicki
Copy link

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

@ClementCardonnel
Copy link

#35 (comment)

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

@navamgupta
Copy link

navamgupta commented Jul 6, 2024

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
Copy link
Contributor

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

@navamgupta
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants