Errors and validation
How Jimp checks the options you pass, and what its common error messages mean.
Jimp checks what you pass it before it touches any pixels, and throws an error when something is wrong. Most image methods validate their options object with zod, a few check single arguments by hand, and reading and writing files has its own set of errors. This page shows what each kind looks like and what the common messages mean.
Synchronous throws and rejected promises
Image methods such as resize and print are synchronous, so they throw.
Anything that loads or encodes, such as Jimp.read, getBuffer, write and loadFont, returns a promise that rejects.
With await, one try/catch handles both:
import { Jimp } from "jimp";
try { const image = await Jimp.read("photo.jpg"); image.resize({ w: 200 }); await image.write("thumbnail.png");} catch (error) { console.error("Couldn't make the thumbnail:", error);}Option validation
Methods that take an options object check it against a zod schema.
If it doesn’t match, the method throws a ZodError, and the image is left unchanged.
image.resize({ w: -100 });The error’s message is the list of problems (“issues”) as JSON:
ZodError: [ { "origin": "number", "code": "too_small", "minimum": 0, "inclusive": true, "path": [ "w" ], "message": "Too small: expected number to be >=0" }]Each issue has a path (the option that’s wrong, here w) and a message saying why.
TypeScript catches most of these mistakes before you run the code, so option errors mostly come from values computed at run time or from plain JavaScript.
Reading the issues
The same issues are on error.issues. You don’t need zod installed to read them: check the error’s name.
try { image.resize({ w: -100 });} catch (error) { if (error instanceof Error && error.name === "ZodError" && "issues" in error) { for (const issue of error.issues as { path: PropertyKey[]; message: string }[]) { console.log(`${issue.path.join(".")}: ${issue.message}`); } // w: Too small: expected number to be >=0 } else { throw error; }}Options with several shapes
Some methods accept several forms of options, for example resize takes { w }, { h } or { w, h }.
When the input matches none of them, you get one invalid_union issue with the message Invalid input, and an errors array with the issues for each form that was tried:
image.resize({});[ { "code": "invalid_union", "errors": [ [{ "path": ["w"], "message": "Invalid input: expected number, received undefined", ... }], [{ "path": ["h"], "message": "Invalid input: expected number, received undefined", ... }] ], "path": [], "message": "Invalid input" }]Read it as “either w or h is missing”.
Common messages
| Message | Meaning |
|---|---|
Invalid input: expected number, received undefined |
A required option is missing. |
Invalid input: expected number, received string |
A number was passed as a string, such as "100". Convert it with Number(). |
Invalid input: expected int, received number |
The option must be a whole number. Round it first. |
Too small: expected number to be >=0 |
The value is below the allowed minimum. |
Too big: expected number to be <=64 |
The value is above the allowed maximum. |
Invalid option: expected one of "nearestNeighbor"|"bilinearInterpolation"|… |
The value isn’t one of an enum’s values. Use the named constant, such as ResizeStrategy.BICUBIC. |
Unrecognized key: "algorithm" |
The method has no option with that name. Some methods reject options they don’t know; check the spelling against the method’s page. |
Invalid input: expected object, received number |
A method that takes an options object got a number, often v0-style positional arguments such as resize(100, 100). See Migrate to v1. |
Some schemas add their own rules with their own messages, for example pHash’s smallerSize must not be larger than size, quantize’s colors must be at least 2 for rgbquant and diff’s threshold must be a number between 0 and 1.
These are still ZodErrors; the custom text is the issue’s message.
Other checks
Methods that take plain arguments rather than an options object check them by hand, and some rules can’t be written as a schema.
These throw an ordinary Error with a short message:
| Method | Message |
|---|---|
blur, gaussian |
r must be a number, r must be greater than 0 |
composite |
The source must be a Jimp image: an object with a bitmap of width, height and data, x and y must be numbers |
composite |
Unknown blend mode "nope", expected one of: srcOver, dstOver, multiply, add, screen, overlay, darken, lighten, hardLight, difference, exclusion |
crop |
The crop region (x: 10, y: 10, w: 2, h: 2) doesn't overlap the 4x3 image, so the result would be empty |
cover, contain |
align must combine at most one HorizontalAlign and one VerticalAlign flag |
getPixelColor, getPixelIndex |
x and y must be numbers (also for NaN) |
setPixelColor |
hex, x and y must be numbers (also for NaN) |
scan |
y must be a number, w must be a number, h must be a number, cb must be a function |
scanIterator |
x and y must be numbers, w and h must be numbers |
hash |
base must be a number between 2 and 64 |
compareHashes |
"be1fcbe0e0601e92" isn't a base 64 hash. Pass the base the hashes were made with to compareHashes, e.g. compareHashes(hash1, hash2, 16)., base must be a number between 2 and 64 |
compareHashes, distanceFromHash |
Can't compare hashes of different lengths (64 and 11). … |
new Jimp |
Unknown color space "nope", expected "srgb" or "display-p3" |
Each method page lists the errors it throws in its Notes.
Colors
Options that take a CSS color string, such as new Jimp({ color }), border, boxShadow, color’s mix and xor and diff’s colors, throw for a string that isn’t a color:
new Jimp({ width: 100, height: 100, color: "nope" });// Error: Invalid color "nope"
new Jimp({ width: 100, height: 100, color: "color(display-p3 foo 0 0)" });// Error: Invalid color "color(display-p3 foo 0 0)": bad channel "foo"The same goes for cssColorToHex.
Changed in 2.0: new Jimp({ color }) and cssColorToHex used to turn an invalid color string into opaque black without an error. The other options above didn’t take CSS color strings before 2.0.
Regions and coordinates
Regions that reach past the edge of the image don’t throw: they’re clamped to the image.
crop, scan, blit, pixelate and convolute only touch the part of the region inside the image.
crop throws only when nothing would be left: the region is entirely outside the image, or w or h is 0 or less.
setPixelColor outside the image does nothing, and getPixelColor outside the image returns the nearest edge pixel.
Both round fractional coordinates and throw for NaN.
Changed in 2.0: out-of-bounds regions used to wrap onto the next row, throw RangeErrors or grow the image. setPixelColor outside the image used to paint the nearest edge pixel, and NaN coordinates used to read or write pixel (0, 0).
Reading images
Jimp.read and Jimp.fromBuffer fail when the data can’t be found, isn’t an image, or is too large.
| Message | Cause and fix |
|---|---|
ENOENT: no such file or directory, open './typo.png' |
In Node.js: the file doesn’t exist. This is Node’s own error, with error.code set to "ENOENT". Check the path; relative paths start from the working directory. |
Could not find MIME for Buffer |
The data isn’t an image format Jimp recognizes: an empty or truncated file, an HTML error page, or a format no installed plugin reads, such as SVG without @jimp/wasm-svg. |
Mime type image/webp does not support decoding |
The format was recognized, but no plugin for it is installed. Add one of the WASM formats with a custom Jimp. |
Expected image data as a Buffer, ArrayBuffer, Uint8Array (or other ArrayBuffer view) or Blob |
Jimp.read or Jimp.fromBuffer got something that isn’t image data, such as a number or a plain object. |
Could not load Buffer from URL: https://… |
fetch failed: the host can’t be reached, or, in the browser, a CORS error. |
HTTP Status 404 for url https://… |
The server answered with an error status. |
Could not load Buffer from https://… |
The server answered, but reading the response body failed. |
In Node.js, Jimp.read only fetches http:, https:, data: and blob: URLs; any other string is read as a file path. In the browser, every string is fetched.
Changed in 2.0: in Node.js, a path that didn’t exist used to be fetched as a URL, failing with Could not load Buffer from URL: ./typo.png.
HTTP and CORS errors
In the browser, Jimp.read loads URLs with fetch, so the browser’s rules apply.
An image on another origin can only be read if its server sends an Access-Control-Allow-Origin header that allows your page.
If it doesn’t, fetch fails and Jimp reports Could not load Buffer from URL: …; the browser’s console shows the underlying CORS error.
Serve the image from your own origin, enable CORS on its server, or load it through your own backend.
loadFont also uses fetch in the browser, and the same rules apply to font files.
Size limits
To protect against huge or malicious files, the JPEG, GIF, TIFF and BMP decoders refuse images over 100 megapixels by default, and so does the @jimp/wasm-svg plugin for the size it would render:
| Format | Message |
|---|---|
| JPEG | maxResolutionInMP limit exceeded by 1MP, maxMemoryUsageInMB limit exceeded by at least 1MB |
| GIF | GIF dimensions 800x600 exceed the maximum resolution of 0.1 megapixels |
| TIFF | TIFF dimensions 800x600 exceed the maximum resolution of 0.1 megapixels |
All GIF frames or TIFF pages, with readFrames |
GIF has 500 frames of 500x500, which together exceed the maximum resolution of 100 megapixels. Decode fewer frames with maxFrames, or raise maxResolutionInMP |
| BMP | BMP dimensions 800x600 exceed the maximum resolution of 0.1 megapixels |
| SVG | SVG dimensions 100000x100000 exceed the maximum resolution of 100 megapixels. Use the width, height or scale option to render it smaller. |
If you trust the file, raise the limit with decode options, as described in Reading Large Images:
const image = await Jimp.read("huge-photo.jpg", { "image/jpeg": { maxResolutionInMP: 200, maxMemoryUsageInMB: 1024 },});Corrupt files fail with the decoder’s own message, such as Invalid GIF dimensions 0x0 or Invalid ICO file: it contains no images.
Asking for an ICO entry that doesn’t exist throws ICO image index 5 is out of range, the file has 1 images.
Writing images
write picks the format from the file extension, and getBuffer from the MIME type you pass.
Cannot write "output": the path has no recognisable file extension to determine the image format: add an extension such as.png.Unsupported MIME type: image/webp: no installed format can encode that type. Use a built-in format or add a WASM format.Unsupported BMP bit depth 7, use 1, 4, 8, 16, 24, 32: the BMPbitPPoption must be one of those values.A 4-bit BMP palette needs 1 to 16 colours, got 20: a BMPpalettehas more colors than the bit depth can index.Jimp can read SVG but can't write it. Write a raster format like PNG (image/png) instead.:@jimp/wasm-svgonly decodes SVG.
Fonts
loadFont explains what went wrong when a font can’t be loaded:
loadFont expects a path or URL to a font file, but received undefined.: v0 code such asloadFont(Jimp.FONT_SANS_32_BLACK). Import the font fromjimp/fonts.loadFont expects a path or URL to a font file, but received the contents of one: …: the file’s text was passed instead of its path.loadFont: font file not found: "…": the path doesn’t exist.loadFont: font page not found: "…": a.pngpage listed in the font isn’t next to the.fntfile.loadFont: font page "…/font_0.tga" is a TGA image; Jimp fonts need PNG pages. …: export the font with PNG pages.loadFont: couldn't load "https://…": HTTP 404 Not FoundandloadFont: couldn't fetch "https://…": …: the font’s URL returned an error status, or the request failed.
The loadFont page lists them all.
See also
- Reading and writing
- Formats
- Reading Large Images
- Using in Browser
- TypeScript: catch option mistakes before running
- Migrate to v1