Tiny CLI helper to inspect and convert HEIC/HEIF files with Sharp compiled
against libheif.
- Node.js 18+ (using nvm keeps versions consistent)
- Build toolchain (
build-essentialon Debian/Ubuntu, Xcode tools on macOS) libvips-devandlibheif-dev(or platform equivalents) — required even if we vendor libvips- Git, curl, Python 3.11+ (for Meson/Ninja helper tooling)
sudo apt update
sudo apt install -y build-essential pkg-config libvips-dev libheif-devbrew install vips libheifnpm installSharp 0.34 ships with libvips 8.17.3+. Debian bookworm only exposes 8.14, so you need a local build that includes libheif.
# run from repo root
python3 -m venv .venv
. .venv/bin/activate
pip install meson ninja
curl -LO https://github.com/libvips/libvips/releases/download/v8.17.3/vips-8.17.3.tar.xz
tar xf vips-8.17.3.tar.xz
mkdir -p vendor/libvips
cd vips-8.17.3
meson setup _build --prefix="$(dirname "$PWD")/vendor/libvips"
ninja -C _build
meson install -C _build
deactivate
rm -rf _buildFeel free to delete the vips-8.17.3 folder and .venv afterwards; the
installed files live in vendor/libvips (already ignored via .gitignore).
PKG_CONFIG_PATH="$(pwd)/vendor/libvips/lib/x86_64-linux-gnu/pkgconfig" \
npm_config_sharp_libvips_local="$(pwd)/vendor/libvips" \
npm_config_sharp_prebuilds=false \
npm run sharp:rebuildWhenever you update libvips/libheif, re-run the command above. The environment
variables ensure that sharp links against the vendored libvips instead of the
older system copy.
convert.js– CLI entry point. Prints metadata, optionally converts, and includes an elapsed-time summary for quick benchmarks.sample1.heif,shelf-christmas-decoration.heic– fixture assets for local testing.vendor/libvips– locally compiled libvips with HEIC support (ignored in git).
-
Install npm dependencies (
npm install). -
Rebuild sharp against the local libvips (section above).
-
Run a metadata-only pass:
npm run metadata -- ./shelf-christmas-decoration.heic
-
Run a conversion to confirm HEIC decoding end-to-end:
npm run convert -- ./shelf-christmas-decoration.heic ./output.jpg
The CLI will print metadata, write the converted file, and emit an
Elapsed time: ...line (millisecond precision) so you can track regression or benchmark numbers. Removeoutput.jpgwhen done.
This repository is intentionally minimal. Its only purpose is to document the
exact steps required to get sharp working with HEIC/HEIF input. To apply the
same setup elsewhere:
-
Build libvips 8.17.3+ with libheif exactly as shown above (Meson/Ninja +
vendor/libvips). Copy the resulting folder into your real project, or host it on your artifact storage. -
Reuse the rebuild command in your actual project when installing sharp:
PKG_CONFIG_PATH="/path/to/vendor/libvips/lib/x86_64-linux-gnu/pkgconfig" \ npm_config_sharp_libvips_local="/path/to/vendor/libvips" \ npm_config_sharp_prebuilds=false \ npm rebuild sharp --build-from-source
-
Smoke-test conversion within that project using any HEIC fixture (you can reuse the two files in this repo or your own assets). If the CLI in your main app prints metadata and converts successfully, libheif support is wired up.
-
Keep system deps documented: whatever environment runs your main app still needs
libheif-dev,libvips-dev, build tools, and Node 18+. Use this README purely to remember the build flags/env vars required to make sharp pick up the HEIC-capable libvips.
output.jpgis git-ignored; delete or overwrite freely during testing.- To benchmark large batches, wrap the CLI in a shell loop and parse the elapsed-time line.
- If
sharp: rebuildfails complaining about libvips, double-check thePKG_CONFIG_PATHandnpm_config_sharp_libvips_localenv vars, then rerun.
All commands run from the project root.
npm run metadata -- ./shelf-christmas-decoration.heicnpm run convert -- ./shelf-christmas-decoration.heic ./output.jpgAccepted output extensions: jpg, jpeg, png, webp.
During conversion the CLI:
- Confirms the input file exists
- Prints key metadata as JSON
- Writes the converted file to the requested path (creating folders if needed)
Two HEIC/HEIF fixtures are bundled for quick testing:
shelf-christmas-decoration.heicsample1.heif
Try converting both to ensure your Sharp build has HEIC support.
Input file not found: Double-check the path (relative paths are resolved from the project root).Output file must use ... extension: Use one of the supported formats listed above.Conversion failed: ... libvips-cpp.so: Reinstalllibvips/libheif, ensure the vendored libvips exists, and rerunnpm run sharp:rebuild.- Rebuild errors referencing
libheif: confirm the system packagelibheif-dev(or Homebrew equivalent) is installed before compiling libvips.