{"record":{"id":"5ae5ab2d742e999c","repo":"lovell/sharp","slug":"expected-valid-width-height-and-channels-to-creat","errorCode":null,"errorMessage":"Expected valid width, height and channels to create a new input image","messagePattern":"Expected valid width, height and channels to create a new input image","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/input.mjs","lineNumber":388,"sourceCode":"          inputDescriptor.createNoiseSigma = 30;\n          if (is.defined(inputOptions.create.noise.sigma)) {\n            if (is.number(inputOptions.create.noise.sigma) && is.inRange(inputOptions.create.noise.sigma, 0, 10000)) {\n              inputDescriptor.createNoiseSigma = inputOptions.create.noise.sigma;\n            } else {\n              throw is.invalidParameterError('create.noise.sigma', 'number between 0 and 10000', inputOptions.create.noise.sigma);\n            }\n          }\n        } else if (is.defined(inputOptions.create.background)) {\n          if (!is.inRange(inputOptions.create.channels, 3, 4)) {\n            throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);\n          }\n          inputDescriptor.createBackground = this._getBackgroundColourOption(inputOptions.create.background);\n        } else {\n          throw new Error('Expected valid noise or background to create a new input image');\n        }\n        delete inputDescriptor.buffer;\n      } else {\n        throw new Error('Expected valid width, height and channels to create a new input image');\n      }\n    }\n    // Create a new image with text\n    if (is.defined(inputOptions.text)) {\n      if (is.object(inputOptions.text) && is.string(inputOptions.text.text)) {\n        inputDescriptor.textValue = inputOptions.text.text;\n        if (is.defined(inputOptions.text.height) && is.defined(inputOptions.text.dpi)) {\n          throw new Error('Expected only one of dpi or height');\n        }\n        if (is.defined(inputOptions.text.font)) {\n          if (is.string(inputOptions.text.font)) {\n            inputDescriptor.textFont = inputOptions.text.font;\n          } else {\n            throw is.invalidParameterError('text.font', 'string', inputOptions.text.font);\n          }\n        }\n        if (is.defined(inputOptions.text.fontfile)) {\n          if (is.string(inputOptions.text.fontfile)) {","sourceCodeStart":370,"sourceCodeEnd":406,"githubUrl":"https://github.com/lovell/sharp/blob/56676c69180a32b468123e090f87bfee30539b49/lib/input.mjs#L370-L406","documentation":"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(...) && ...)`.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nsharp({ create: { width: Number(req.query.w), height: 100, channels: 3, background: 'red' } })\n\n// after\nconst w = Number(req.query.w);\nif (!Number.isInteger(w) || w < 1) throw new Error('bad width');\nsharp({ create: { width: w, height: 100, channels: 3, background: 'red' } })","handlingStrategy":"validation","validationCode":"function safeCreate({ width, height, channels, ...rest }) {\n  const ok = Number.isInteger(width) && width >= 1 && width <= 1e8 &&\n             Number.isInteger(height) && height >= 1 && height <= 1e8 &&\n             Number.isInteger(channels);\n  if (!ok) throw new Error('create requires integer width,height in [1,1e8] and integer channels');\n  return sharp({ create: { width, height, channels, ...rest } });\n}","typeGuard":"function isValidCreateDims(c) {\n  return Number.isInteger(c.width) && c.width >= 1 && c.width <= 1e8 &&\n    Number.isInteger(c.height) && c.height >= 1 && c.height <= 1e8 &&\n    Number.isInteger(c.channels);\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["create","input","validation","dimensions"],"backgroundTag":null,"analyzedSha":"56676c69180a32b468123e090f87bfee30539b49","analyzedAt":"2026-08-13T04:44:31.201Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}