lovell/sharp · error · Error
Expected valid width, height and channels to create a new in
Error message
Expected valid width, height and channels to create a new input image
What it means
Thrown when inputOptions.create is provided but the outer validation of width, height, and channels fails. Sharp requires create.width and create.height to be integers in range 1-100000000 and create.channels to be an integer; if any of these conditions is false, the whole create block is rejected and the buffer is not consumed. This is the else of the big compound `if (is.object(create) && is.integer(width) && is.inRange(...) && ...)`.
Source
Thrown at lib/input.mjs:388
inputDescriptor.createNoiseSigma = 30;
if (is.defined(inputOptions.create.noise.sigma)) {
if (is.number(inputOptions.create.noise.sigma) && is.inRange(inputOptions.create.noise.sigma, 0, 10000)) {
inputDescriptor.createNoiseSigma = inputOptions.create.noise.sigma;
} else {
throw is.invalidParameterError('create.noise.sigma', 'number between 0 and 10000', inputOptions.create.noise.sigma);
}
}
} else if (is.defined(inputOptions.create.background)) {
if (!is.inRange(inputOptions.create.channels, 3, 4)) {
throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);
}
inputDescriptor.createBackground = this._getBackgroundColourOption(inputOptions.create.background);
} else {
throw new Error('Expected valid noise or background to create a new input image');
}
delete inputDescriptor.buffer;
} else {
throw new Error('Expected valid width, height and channels to create a new input image');
}
}
// Create a new image with text
if (is.defined(inputOptions.text)) {
if (is.object(inputOptions.text) && is.string(inputOptions.text.text)) {
inputDescriptor.textValue = inputOptions.text.text;
if (is.defined(inputOptions.text.height) && is.defined(inputOptions.text.dpi)) {
throw new Error('Expected only one of dpi or height');
}
if (is.defined(inputOptions.text.font)) {
if (is.string(inputOptions.text.font)) {
inputDescriptor.textFont = inputOptions.text.font;
} else {
throw is.invalidParameterError('text.font', 'string', inputOptions.text.font);
}
}
if (is.defined(inputOptions.text.fontfile)) {
if (is.string(inputOptions.text.fontfile)) {View on GitHub (pinned to 56676c6918)
Solutions
- Ensure create.width and create.height are integers within 1 to 100000000.
- Always set create.channels explicitly (1 grayscale, 3 RGB, 4 RGBA).
- Coerce and validate inputs before building the create object: Number.isInteger(width) && width >= 1.
- If you conditionally build create, make sure all three fields are present in the final object.
Example fix
// before
sharp({ create: { width: Number(req.query.w), height: 100, channels: 3, background: 'red' } })
// after
const w = Number(req.query.w);
if (!Number.isInteger(w) || w < 1) throw new Error('bad width');
sharp({ create: { width: w, height: 100, channels: 3, background: 'red' } }) Defensive patterns
Strategy: validation
Validate before calling
function safeCreate({ width, height, channels, ...rest }) {
const ok = Number.isInteger(width) && width >= 1 && width <= 1e8 &&
Number.isInteger(height) && height >= 1 && height <= 1e8 &&
Number.isInteger(channels);
if (!ok) throw new Error('create requires integer width,height in [1,1e8] and integer channels');
return sharp({ create: { width, height, channels, ...rest } });
} Type guard
function isValidCreateDims(c) {
return Number.isInteger(c.width) && c.width >= 1 && c.width <= 1e8 &&
Number.isInteger(c.height) && c.height >= 1 && c.height <= 1e8 &&
Number.isInteger(c.channels);
} Prevention
- Always set width, height, and channels explicitly.
- Coerce dimension inputs from user data with Number() and validate with Number.isInteger.
- Avoid defaulting dimensions to undefined.
When it happens
Trigger: create: { width: 0 } (out of range), create: { width: 100.5 } (non-integer), create: { width: 100, height: 100 } (channels missing), create: { width: '100', height: 100, channels: 3 } (string width), or create that is not even an object.
Common situations: Parsing dimensions from user input as strings and not coercing. Letting width/height default to undefined. Off-by values like width: -1 or width: 0 from a subtraction. Channels omitted because the developer assumed a default.
Related errors
- Expected width, height and channels for raw pixel input
- Expected create.height ${inputOptions.create.height} to be a
- Expected noise to be an object
- Only gaussian noise is supported at the moment
- Expected valid noise or background to create a new input ima
AI-assisted analysis of lovell/sharp@56676c6918 (2026-08-13).
Data as JSON: /api/errors/5ae5ab2d742e999c.
Report an issue: GitHub.