{"library":"sharp","title":"Sharp: High-Performance Image Processor","description":"Sharp is a high-performance Node.js module designed for fast image processing, offering extensive support for resizing, converting, and manipulating JPEG, PNG, WebP, AVIF, GIF, TIFF, and SVG images. It leverages the native libvips library, making it one of the fastest solutions available for image operations in Node.js environments. The current stable version is 0.34.5, with frequent updates including patch releases and release candidates for upcoming major versions like 0.35.0. It's differentiated by its speed, low memory footprint, and broad format compatibility, making it suitable for high-volume image processing tasks such as thumbnail generation and on-the-fly image transformations for web applications. The project actively maintains support for recent Node.js LTS versions and ships with TypeScript types.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install sharp"],"cli":null},"imports":["import sharp from 'sharp';","import sharp, { type Sharp } from 'sharp';","import type { FormatEnum } from 'sharp';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import sharp from 'sharp';\nimport path from 'path';\nimport fs from 'fs/promises';\n\nconst inputImagePath = './input.jpg'; // Ensure this file exists for the example\nconst outputDirectory = './output';\n\nasync function processImage() {\n  try {\n    await fs.mkdir(outputDirectory, { recursive: true });\n\n    // Create a dummy input file if it doesn't exist for the example\n    try {\n      await fs.access(inputImagePath);\n    } catch (error) {\n      console.log('Creating a dummy input.jpg for the example...');\n      await sharp({ create: { width: 500, height: 300, channels: 3, background: { r: 255, g: 100, b: 50 } } })\n        .jpeg({ quality: 80 })\n        .toFile(inputImagePath);\n    }\n\n    const outputWebPPath = path.join(outputDirectory, 'output-resized.webp');\n    const outputJpgPath = path.join(outputDirectory, 'output-thumb.jpg');\n\n    console.log(`Processing ${inputImagePath}...`);\n\n    // Resize to 320x240 and convert to WebP\n    await sharp(inputImagePath)\n      .resize(320, 240, { fit: 'cover' })\n      .webp({ quality: 90 })\n      .toFile(outputWebPPath);\n    console.log(`Image resized and converted to WebP: ${outputWebPPath}`);\n\n    // Create a 150px square thumbnail, grayscale, and convert to JPEG\n    await sharp(inputImagePath)\n      .resize(150, 150, { fit: 'cover' })\n      .grayscale()\n      .jpeg({ quality: 75 })\n      .toFile(outputJpgPath);\n    console.log(`Image thumbnailed and converted to JPEG: ${outputJpgPath}`);\n\n  } catch (error) {\n    console.error('Error processing image:', error);\n  }\n}\n\nprocessImage();\n","lang":"typescript","description":"This quickstart demonstrates how to use sharp to resize an image, convert it to different formats (WebP and JPEG), and apply common transformations like grayscale, saving the results to files. It also handles creating a dummy input file if needed.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}