Sharp Throwing 502 Bad Gateway Errors When Running In Production

Hey, I’m creating this post because I cannot get sharp to run with Netlify. I’ve tried everything I could find on the forums and on GitHub, and it still isn’t working. My netlify.toml looks like this currently, mirroring what I was able to find on the forums:

[functions]
  external_node_modules = ["sharp"]
  included_files = ["node_modules/sharp/**/*"]

I tried it with ./ before the node_modules and as is for the included files, I tried running the command to install sharp and the correct(?) versions of it cross platform in my build command, I tried to force install the cross platform versions of sharp, I tried the same with the --legacy-peer-deps flag and -f to force install, I tried to delete my package-lock.json, I tried deleting the lock and node modules, nothing seems to work. What am I doing wrong? I tried every which way I could think to install it, it only works locally. When pushing it to Netlify/AWS functions it doesn’t. What is happening?

The funny thing is, the files which are throwing the 502 errors, don’t even require sharp. But in the network tab I get the error:

"errorMessage": "Could not load the "sharp" module using the linux-x64 runtime
Possible solutions:
- Ensure optional dependencies can be installed:
    npm install --include=optional sharp..."

and so on, make sure it’s included cross platform, consult the install page, etc.

Any help is very greatly appreciated, I have no idea what to do to fix this, given Sharp is compiled in C++ and I’m not exactly a great C++ programmer. Let alone one that knows how to merge JS and C++ together.

Update: I tried to alias sharp as mentioned in this thread to no avail.

Quick update, here is a link to the live deploy as well as the repository, still haven’t had any luck with getting Sharp running in production.

Update, I’ve tried some more troubleshooting steps and still can’t get it. I’ve created a installSharp script in my package.json which runs npm install --cpu=x64 --os=linux --libc=glibc sharp && npm install --cpu=x64 --os=linux --libc=musl sharp, allegedly this is how you install Sharp on linux distros. I’ve been trying this and I’m still running into issues. On Sharp’s install guide it sates that the package-lock.json can sometimes cause problems, so I’ve added that to my .gitignore so it shouldn’t try to install anything unnecessary/no dependancies SHOULD break (to my knowledge). Unfortunately, this still isn’t working, running npm run installSharp && npm i && npm run build as my build command is still yielding a 502 bad gateway error. My full netlify.toml looks like

[functions]
  external_node_modules = ["sharp"]
  included_files = ["node_modules/sharp/**/*"]

[[redirects]]
  from = "/api/*"
  to = "/.netlify/functions/server/api/:splat"
  status = 200

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

[build]
  functions = "build/server"
  command = "npm run installSharp && npm i && npm run build"
  publish = "build"

and it seems to be building fine, however I’m still unable to use Sharp.

Any insight and or help is very greatly appreciated. Thank you!

I also tried running Sharp using WASM, which according to the forums should be supported, but to no avail. I was unable to get it up and running with WASM or with the Linux binaries (I’m nearly positive the binaries are being added, I can’t imagine they’re not, if I run installSharp after deleting the node_modules/sharp folder, Sharp no longer runs on my mac even though the node_modules/sharp folder exists)

Ok, so I created a docker image to run the site as netlify would on build, I think. I got it to build and to run alright, the only issue is that it runs perfectly fine as is through the docker file, which to my knowledge is running the linux x64 architecture.

Thank you in advance for any help, it would be very much appreciated.

This appears to work for me: hrishikesh-k/f-118984 (github.com)
Site: f-118984.netlify.app/.netlify/functions/sharp

it works! Wow, thank you so much, I was doing everything to include node_modules/sharp, when I should’ve included

[functions]
  included_files = ["node_modules/@img/**/*"]

I did not realize that is where the cross platform binaries were/are, thank you hrishikesh!

So simple but you’re incredibly smart for finding that, I thought they were in the sharp folder, for 4 days.