{"id":10421,"library":"sharp","title":"High performance Node.js image processing","description":"Sharp is a high-performance Node.js module designed for fast image processing, capable of resizing, cropping, and transforming JPEG, PNG, WebP, GIF, AVIF, and TIFF images. The current stable version is 0.34.5, and the library is actively maintained with regular updates and new features, often preceded by release candidates.","status":"active","version":"0.34.5","language":"javascript","source_language":"en","source_url":"git://github.com/lovell/sharp","tags":["javascript","jpeg","png","webp","avif","tiff","gif","svg","jp2","typescript"],"install":[{"cmd":"npm install sharp","lang":"bash","label":"npm"},{"cmd":"yarn add sharp","lang":"bash","label":"yarn"},{"cmd":"pnpm add sharp","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"wrong":"const sharp = require('sharp')","symbol":"sharp","correct":"import sharp from 'sharp'"}],"quickstart":{"code":"import sharp from 'sharp';\nimport { createWriteStream, readFileSync } from 'fs';\n\nasync function processImage() {\n  const inputPath = 'input.jpg'; // Replace with your input image path\n  const outputPath = 'output.webp';\n\n  // Create a dummy input image if it doesn't exist for demonstration\n  try {\n    readFileSync(inputPath);\n  } catch (error) {\n    const placeholderImage = Buffer.from('<svg width=\"100\" height=\"100\" viewBox=\"0 0 100 100\"><rect width=\"100\" height=\"100\" fill=\"#f0f0f0\" /><text x=\"50%\" y=\"50%\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-size=\"20\" fill=\"#333\">Hello</text></svg>');\n    await sharp(placeholderImage)\n      .toFile(inputPath);\n    console.log(`Created dummy input.jpg at ${inputPath}`);\n  }\n\n  try {\n    await sharp(inputPath)\n      .resize(300, 200, { fit: 'cover' })\n      .webp({ quality: 80 })\n      .toFile(outputPath);\n    console.log(`Image processed and saved to ${outputPath}`);\n  } catch (error) {\n    console.error(`Error processing image: ${error}`);\n  }\n}\n\nprocessImage();","lang":"typescript","description":"Resizes an input image (or creates a placeholder if none exists) to 300x200 pixels, converts it to WebP format with 80% quality, and saves it to 'output.webp'."},"warnings":[{"fix":"Upgrade your Node.js environment to version 20.9.0 or later.","message":"Sharp v0.35.0 and later require Node.js 20.9.0 or higher. Node.js 18 support is dropped.","severity":"breaking","affected_versions":">=0.35.0"},{"fix":"If you need to compile sharp from source, use `npm run build` or `yarn build` explicitly.","message":"The automatic `install` script in `package.json` for compiling from source has been removed.","severity":"breaking","affected_versions":">=0.35.0"},{"fix":"Update your AVIF quality configuration, replacing `ssim` parameters with `iq` when using `.avif()`.","message":"AVIF output quality metrics have changed from `ssim` to `iq` (SSIMULACRA2-based) in v0.35.0.","severity":"breaking","affected_versions":">=0.35.0"},{"fix":"Sharp will now throw errors by default for invalid inputs. Ensure your code handles exceptions thrown by sharp operations.","message":"The `failOnError` constructor property has been removed.","severity":"breaking","affected_versions":">=0.35.0"},{"fix":"Remove or update any code that relies on accessing `paletteBitDepth` from the image metadata object.","message":"The `paletteBitDepth` property is no longer included in the `metadata` response.","severity":"breaking","affected_versions":">=0.35.0"}],"env_vars":null,"last_verified":"2026-04-18T00:00:00.000Z","next_check":"2026-07-17T00:00:00.000Z","problems":[{"fix":"Run `npm install sharp` or `yarn add sharp` to ensure the package and its native dependencies are correctly installed.","cause":"The 'sharp' package was not installed successfully or is missing from `node_modules`.","error":"Error: Cannot find module 'sharp'"},{"fix":"Check the `engines` field in sharp's `package.json` or consult the documentation for compatible Node.js versions. Upgrade Node.js accordingly (e.g., to 20.9.0+ for sharp >= 0.35.0).","cause":"You are using a version of Node.js that is not supported by your installed 'sharp' version, often due to a major sharp version upgrade.","error":"Error: The 'sharp' module is incompatible with the current Node.js version."},{"fix":"Verify that the input file exists, is readable, and is a valid image in a format supported by sharp (JPEG, PNG, WebP, etc.).","cause":"The input file provided to `sharp()` is either corrupted, not a supported image format, or its path is incorrect.","error":"Error: Input file is not a valid image"},{"fix":"For very large images, consider streaming input/output or increasing Node.js's memory limit using `node --max-old-space-size=4096 script.js` (adjust `4096` as needed).","cause":"Processing very large images or performing complex operations on multiple images simultaneously can exhaust Node.js's memory limit.","error":"RangeError: Allocation failed - JavaScript heap out of memory"},{"fix":"Ensure that the directory where you are trying to save the output file has write permissions for the user running the Node.js process. You might need to change directory permissions or choose a different output path.","cause":"The Node.js process does not have the necessary write permissions for the specified output directory or file.","error":"Error: EACCES: permission denied, open 'output.webp'"}],"ecosystem":"npm"}