5

When I add this piece of code from sharp package (that is a image processing package):

await sharp(req.file.path)
    .resize(500)
    .jpeg({ quality: 50 })
    .toFile(path.resolve(req.file.destination, “resized”, filename));

I get this error:

Illegal instruction (core dumped)

When I remove it, everything is ok. I’m very confused. What is wrong with that code?

Arman Zanjani
  • 197
  • 11
  • Theres nothing wrong with your code. That's a bug in `sharp` - apparently it tries to use CPU instructions that are not supported on your CPU. Try an older version of `sharp`. – Tomalak May 18 '21 at 06:22
  • Thank you :) Maybe I use another library like ```jimp```. – Arman Zanjani May 18 '21 at 06:27
  • You could do that as well. But I bet there is a `sharp` version that works. Strategy: Try the oldest version you can find. If that works, find the "middle" between that and the latest version, try again. Keep doing that until you have the latest version that works on your CPU, it should only take 3 or 4 tries to pinpoint it. – Tomalak May 18 '21 at 06:30
  • Yes. This is also a good option. So I will give ```sharp``` another chance :) – Arman Zanjani May 18 '21 at 06:34
  • ...and if you have put in the 20 minutes it takes and found the latest version that works, you could be extra nice and open an [issue at their GitHub repository](https://github.com/lovell/sharp/issues), include the "last version it works" number, your exact CPU type, and the error message - and maybe they will fix it. – Tomalak May 18 '21 at 06:38
  • Yes, I will do it. it seems that version 0.25.0 works for me. I keep checking later versions. When I found it I will open an issue. Thank you again. – Arman Zanjani May 18 '21 at 06:42

1 Answers1

8

Through my conversation with @Tomalak, it seems that the latest version of sharp is not compatible with my CPU:

Theres nothing wrong with your code. That's a bug in sharp - apparently it tries to use CPU instructions that are not supported on your CPU. Try an older version of sharp. – Tomalak

I tried a couple of older versions. The latest version that works for me is 0.27.2. you can install it using this command:

npm i sharp@0.27.2

I also opened an issue on their GitHub page: https://github.com/lovell/sharp/issues/2723#issue-894046354

My environment (npx envinfo --binaries --system):

System:
  OS: Linux 5.4 Linux Mint 20 (Ulyana)
  CPU: (2) x64 Pentium(R) Dual-Core  CPU      E5300  @ 2.60GHz
  Memory: 172.51 MB / 3.84 GB
  Container: Yes
  Shell: 3.2.2 - /usr/bin/fish
Binaries:
  Node: 10.19.0 - /usr/bin/node
  npm: 7.6.1 - /usr/local/bin/npm
Tomalak
  • 332,285
  • 67
  • 532
  • 628
Arman Zanjani
  • 197
  • 11
  • Nice! I hope they come up with a fix. The E5300 is pretty old, but personally I don't think there is a good reason not to support it. Whatever they changed after `0.27.2`, I suppose they could add some sort of conditional compilation to their C code to mitigate the issue. – Tomalak May 18 '21 at 07:25
  • Yes. My computer is almost 12 years old and it looks like my little brother :) I hope they fix the bug. – Arman Zanjani May 18 '21 at 07:31
  • ...but if they don't, well. All you lose is new features after version 0.27.2, and maybe you don't need those. – Tomalak May 18 '21 at 07:40
  • 1
    I don't care about new features. – Arman Zanjani May 18 '21 at 07:51
  • I spent 9 days trying to figure out what was wrong with my gatsby project after adding the sharp library. His answer solved my problem and gave me peace of mind. – Andre Bellafronte May 15 '22 at 00:19
  • Like Andre I spent many days trying to pinpoint an "Illegal Instruction" error on my application. That's not much to go off of. Turns our it was Sharp. I tried many versions and just as you 0.27.2 solved my issue as well. Looks like it's time to find another package. – Jb. Jan 18 '23 at 04:39