{"record":{"id":"675609571ada7849","repo":"lovell/sharp","slug":"invalid-input","errorCode":null,"errorMessage":"Invalid input","messagePattern":"Invalid input","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/constructor.mjs","lineNumber":227,"sourceCode":" * @param {string} [options.join.valign='top'] - vertical alignment style for images joined vertically (`'top'`, `'centre'`, `'center'`, `'bottom'`).\n * @param {Object} [options.tiff] - Describes TIFF specific options.\n * @param {number} [options.tiff.subifd=-1] - Sub Image File Directory to extract for OME-TIFF, defaults to main image.\n * @param {Object} [options.svg] - Describes SVG specific options.\n * @param {string} [options.svg.stylesheet] - Custom CSS for SVG input, applied with a User Origin during the CSS cascade.\n * @param {boolean} [options.svg.highBitdepth=false] - Set to `true` to render SVG input at 32-bits per channel (128-bit) instead of 8-bits per channel (32-bit) RGBA.\n * @param {Object} [options.pdf] - Describes PDF specific options. Requires the use of a globally-installed libvips compiled with support for PDFium, Poppler, ImageMagick or GraphicsMagick.\n * @param {string|Object} [options.pdf.background] - Background colour to use when PDF is partially transparent. Parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.\n * @param {Object} [options.openSlide] - Describes OpenSlide specific options. Requires the use of a globally-installed libvips compiled with support for OpenSlide.\n * @param {number} [options.openSlide.level=0] - Level to extract from a multi-level input, zero based.\n * @param {Object} [options.jp2] - Describes JPEG 2000 specific options. Requires the use of a globally-installed libvips compiled with support for OpenJPEG.\n * @param {boolean} [options.jp2.oneshot=false] - Set to `true` to decode tiled JPEG 2000 images in a single operation, improving compatibility.\n * @returns {Sharp}\n * @throws {Error} Invalid parameters\n */\nconst Sharp = function (input, options) {\n  // biome-ignore lint/complexity/noArguments: constructor factory\n  if (arguments.length === 1 && !is.defined(input)) {\n    throw new Error('Invalid input');\n  }\n  if (!(this instanceof Sharp)) {\n    return new Sharp(input, options);\n  }\n  stream.Duplex.call(this);\n  this.options = {\n    // resize options\n    topOffsetPre: -1,\n    leftOffsetPre: -1,\n    widthPre: -1,\n    heightPre: -1,\n    topOffsetPost: -1,\n    leftOffsetPost: -1,\n    widthPost: -1,\n    heightPost: -1,\n    width: -1,\n    height: -1,\n    canvas: 'crop',","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/lovell/sharp/blob/56676c69180a32b468123e090f87bfee30539b49/lib/constructor.mjs#L209-L245","documentation":"Thrown by the Sharp constructor when it is invoked with exactly one argument that is not a defined value (undefined or null). The factory guards this up front because every downstream input path (file path, Buffer, Stream, raw pixel data) requires a concrete input; an undefined sole argument almost always indicates a variable that was never assigned. Note arguments.length === 1 is the discriminator, so sharp() with no args is allowed (creates a stream-based pipeline) but sharp(undefined) is not.","triggerScenarios":"Calling sharp(undefined), sharp(null), or sharp(someVariable) where someVariable was never assigned. Passing options as the first arg by mistake: sharp({ density: 72 }) instead of sharp(input, { density: 72 }). Destructuring a missing field: const { path } = req.body; sharp(path) where path is undefined.","commonSituations":"Form/file-upload handlers where the file path comes from an unvalidated request field. Migration from a library that accepted an options object as the sole argument. Conditional pipelines built as sharp(maybeInput) where maybeInput can be undefined.","solutions":["Ensure the first argument is a real input: a file path string, a Buffer, a Stream, a Uint8Array, or an array of images to join.","If you genuinely want a stream-based pipeline with no initial input, call sharp() with zero arguments.","When passing options, use the second parameter: sharp(input, options), never sharp(options).","Add a guard before construction: if (!input) throw new Error('input required'); to fail with your own message."],"exampleFix":"// before\nconst img = sharp(req.body.path); // path is undefined\n\n// after\nconst path = req.body.path;\nif (!path) throw new Error('image path required');\nconst img = sharp(path);","handlingStrategy":"validation","validationCode":"function sharpSafe(input, options) {\n  if (arguments.length >= 1 && (input === undefined || input === null)) {\n    throw new Error('sharp: input is required (pass a path, Buffer, Stream, or array)');\n  }\n  return sharp(input, options);\n}","typeGuard":"function isValidSharpInput(input) {\n  return typeof input === 'string' || Buffer.isBuffer(input) || input instanceof Uint8Array ||\n    (typeof input === 'object' && input !== null && typeof input.pipe === 'function') ||\n    Array.isArray(input);\n}","tryCatchPattern":null,"preventionTips":["Always validate that an input variable is truthy before passing it to sharp.","Use zero-arg sharp() only when you intentionally want a stream pipeline.","Never pass an options object as the first argument."],"tags":["constructor","input","validation","undefined-argument"],"backgroundTag":null,"analyzedSha":"56676c69180a32b468123e090f87bfee30539b49","analyzedAt":"2026-08-13T04:44:31.201Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}