NodeJS: Illegal instruction (core dumped) Error after using sharp library
Asked Answered
C

2

6

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?

Caban answered 18/5, 2021 at 6:12 Comment(6)
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.Sled
Thank you :) Maybe I use another library like jimp.Caban
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.Sled
Yes. This is also a good option. So I will give sharp another chance :)Caban
...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, include the "last version it works" number, your exact CPU type, and the error message - and maybe they will fix it.Sled
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.Caban
C
9

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 [email protected]

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
Caban answered 18/5, 2021 at 7:15 Comment(8)
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.Sled
Yes. My computer is almost 12 years old and it looks like my little brother :) I hope they fix the bug.Caban
...but if they don't, well. All you lose is new features after version 0.27.2, and maybe you don't need those.Sled
I don't care about new features.Caban
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.Tenebrae
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.Metritis
Interesting thing is that it works on windows but does not work on linux mint (for me)Kor
This can be a kernel or old CPU issue. 2 years after a brand new version of the sharp library the problem persists. Some system dump for the eternity ( npx envinfo --binaries --system): System: OS: Linux 5.15 Linux Mint 21.1 (Vera) CPU: (6) x64 AMD Phenom(tm) II X6 1100T Processor Memory: 5.72 GB / 31.34 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node Yarn: 1.22.19 - ~/.yarn/bin/yarn npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npmTenebrae
T
0

For Linux x64 Sharp needs CPU with SSE4.2 instructions support. CPUs launched below 2019 may not have this version available.

Sources:

Tenebrae answered 19/3 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.