Unable to upload image to Ghost CMS with API error 500 and message "Internal server error, cannot upload image.

Unable to upload image to Ghost CMS with API error 500 and message "Internal server error, cannot upload image.

Long story short, a nodejs module was missing. This module is called "sharp".

How do I know this? Well, in case of an error with ghost, you can easily go into debug mode by stopping host with ghost stop and then running it in debug mode with ghost run.

# ghost run


Love open source? We’re hiring JavaScript Engineers to work on Ghost full-time.
https://careers.ghost.org

The `ghost run` command is used by the configured Ghost process manager and for debugging. If you're not running this to debug something, you should run `ghost start` instead.
[2023-04-11 18:19:09] INFO Ghost is running in production...
[2023-04-11 18:19:09] INFO Your site is now available on https://pro-it.rocks/
[2023-04-11 18:19:09] INFO Ctrl+C to shut down
[2023-04-11 18:19:09] INFO Ghost server started in 1.282s
[2023-04-11 18:19:10] INFO Database is in a ready state.
[2023-04-11 18:19:10] INFO Ghost database ready in 2.535s

Now doing the same activity as normal, in my case uploading image, it would produce exact error in shell:



[2023-04-11 17:48:21] ERROR "POST /ghost/api/admin/images/upload/" 500 889ms

Sharp wasn't installed

Error ID:
    0cf47bc0-d891-11ed-9cd4-317a1372599b

Error Code:
    SHARP_INSTALLATION

----------------------------------------

Error: Cannot find module 'sharp'
    at Object.resizeFromPath (/var/www/node/pro-it.rocks/versions/5.41.0/node_modules/@tryghost/image-transform/lib/transform.js:127:31)
Require stack:

So, how on earth did Ghost itself fail to install the required (rather crucial) image manipulation module when it updated? In my case, it was due to a dependency that wasn't compatible with my OS of choice (NetBSD). I discovered this by trying to install it manually using npm:

 npm install sharp
npm ERR! code 1
npm ERR! path /var/www/node/pro-it.rocks/node_modules/sharp
npm ERR! command failed
npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
npm ERR! sharp: Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.14.2/libvips-8.14.2-netbsd-x64.tar.br
npm ERR! sharp: Please see https://sharp.pixelplumbing.com/install for required dependencies
npm ERR! sharp: Installation error: Prebuilt libvips 8.14.2 binaries are not yet available for netbsd-x64

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-04-11T17_54_41_932Z-debug-0.log

That's odd, as I was browsing the github of libvips here: https://github.com/lovell/sharp-libvips/releases and no release has a special version for netbsd...

Well, luckily for those of us who like to compile everything from source, and with the power of pkgsrc, and another power of WIP packages (Work In Progress), we can install it from source and not be dependent on precompiled packages. Let's just go to /usr/pkgsrc/wip/libvips and issue the command make install clean. After a while we have it compiled and installed on the NetBSD file system:

=> Generating post-install file lists
=> Checking file-check results for libvips-8.14.2
=> Creating binary package /usr/pkgsrc/wip/libvips/work/.packages/libvips-8.14.2.tgz
===> Building binary package for libvips-8.14.2
=> Creating binary package /usr/pkgsrc/packages/All/libvips-8.14.2.tgz
===> Installing binary package of libvips-8.14.2
===> Cleaning for libvips-8.14.2

After that, we can install the sharp module in the root directory of our Ghost CMS.

# npm install sharp

changed 1 package, and audited 47 packages in 32s

10 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Then restart it with a normal start or restart command and voilà, the images are working again!

# ghost start

Love open source? We’re hiring JavaScript Engineers to work on Ghost full-time.
https://careers.ghost.org


✔ Checking system Node.js version - found v14.21.1
✔ Checking current folder permissions
✔ Validating config
✔ Checking memory availability
✔ Checking binary dependencies
✔ Starting Ghost: pro-it.rocks