Registry / data / sharp
library1.0.2jsnpmunverified

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.

npm install sharp
INSTALL
IMPORT
SIG · SHARP
S
sharp
datajavascriptv1.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sharp
import sharp from 'sharp';
const sharp = require('sharp');
The primary entry point is the default export, which is a function that can be called directly with a buffer or file path.
Sharp
import sharp, { type Sharp } from 'sharp';
The `Sharp` type is useful for type hinting when chaining operations on an image instance.
FormatEnum
import type { FormatEnum } from 'sharp';
Type imports like `FormatEnum` are used for strictly typing format options and are often omitted in plain JavaScript.

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.

import sharp from 'sharp'; import path from 'path'; import fs from 'fs/promises'; const inputImagePath = './input.jpg'; // Ensure this file exists for the example const outputDirectory = './output'; async function processImage() { try { await fs.mkdir(outputDirectory, { recursive: true }); // Create a dummy input file if it doesn't exist for the example try { await fs.access(inputImagePath); } catch (error) { console.log('Creating a dummy input.jpg for the example...'); await sharp({ create: { width: 500, height: 300, channels: 3, background: { r: 255, g: 100, b: 50 } } }) .jpeg({ quality: 80 }) .toFile(inputImagePath); } const outputWebPPath = path.join(outputDirectory, 'output-resized.webp'); const outputJpgPath = path.join(outputDirectory, 'output-thumb.jpg'); console.log(`Processing ${inputImagePath}...`); // Resize to 320x240 and convert to WebP await sharp(inputImagePath) .resize(320, 240, { fit: 'cover' }) .webp({ quality: 90 }) .toFile(outputWebPPath); console.log(`Image resized and converted to WebP: ${outputWebPPath}`); // Create a 150px square thumbnail, grayscale, and convert to JPEG await sharp(inputImagePath) .resize(150, 150, { fit: 'cover' }) .grayscale() .jpeg({ quality: 75 }) .toFile(outputJpgPath); console.log(`Image thumbnailed and converted to JPEG: ${outputJpgPath}`); } catch (error) { console.error('Error processing image:', error); } } processImage();
Debug
Known issues
breakingSharp v0.35.0 (and its release candidates) drops support for Node.js 18. Users must upgrade to Node.js >= 20.9.0.
fix
Upgrade your Node.js environment to version 20.9.0 or higher. For production environments, consider moving to the latest LTS release of Node.js.
affects: >=0.35.0-rc.0
breakingStarting with v0.35.0, the `install` script has been removed from `package.json`. Compiling from source is now an opt-in process using the `build` script.
fix
If you relied on `sharp` compiling from source during `npm install`, you will now need to explicitly run `npm run build` or `npm install --build-from-source` (pre-v0.34.5) if pre-built binaries are not suitable for your environment. Check the official documentation for the updated build process.
affects: >=0.35.0-rc.0
breakingThe `failOnError` constructor property has been removed, as well as `paletteBitDepth` from the `metadata` response, and several properties from `sharpen`.
fix
Review your code for usage of `failOnError`, `paletteBitDepth`, and the deprecated `sharpen` properties. Replace them with the recommended alternatives or remove them if no longer necessary. Consult the sharp changelog for specific replacements.
affects: >=0.35.0-rc.0
gotchaSharp is a native Node.js module and requires `libvips` to be available. While it often ships with pre-compiled binaries, certain environments or specific `libvips` features might necessitate building from source, which can fail due to missing system dependencies (e.g., C++ compilers, development headers).
fix
If installation fails, check the sharp installation guide for required system dependencies for your operating system. For `npm install --build-from-source` (deprecated in v0.34.5, replaced by `npm run build` in v0.35.0+), ensure you have `node-gyp` prerequisites installed (Python, C++ compiler).
affects: >=0.1.0
gotchaMemory usage can be a concern when processing very large images, especially with chained operations. While `sharp` is memory-efficient, unoptimized usage can still lead to 'JavaScript heap out of memory' errors in Node.js.
fix
For extremely large images, consider processing them in chunks or using input streams to manage memory more effectively. Ensure you are streaming or writing to file as soon as possible, rather than keeping large buffers in memory across multiple operations. Monitor Node.js heap usage and consider increasing `--max-old-space-size` if necessary, though optimizing code is preferable.
affects: >=0.1.0
Errors
Common errors & fixes
Error: EACCES: permission denied, open 'path/to/output.jpg'
The Node.js process does not have sufficient write permissions to the specified output directory or file.
fix
Ensure the directory where you are trying to write the image exists and that the Node.js process has write permissions for it. You might need to change directory permissions (e.g., `chmod 777 /path/to/output`) or run your application with appropriate user privileges.
Error: Input file contains unsupported image format
Sharp was unable to identify or process the format of the input image file. This could be due to a corrupted file, an unrecognized extension, or a format not supported by the underlying libvips library.
fix
Verify the input file's integrity and ensure its format is one of the supported types (JPEG, PNG, WebP, AVIF, GIF, TIFF, SVG, JP2). Check for common issues like incorrect file extensions that don't match the actual file content.
Error: Cannot find module 'sharp'
The `sharp` package or its native bindings were not correctly installed or are not accessible in the current environment.
fix
Run `npm install sharp` or `yarn add sharp`. If this persists, clear your npm cache (`npm cache clean --force`), delete `node_modules`, and `package-lock.json`, then reinstall. If it's a native module compilation issue, check system dependencies required for `libvips`.
Error: VipsOperation: class 'jpegload' not found
This usually indicates an issue with the underlying `libvips` library installation or configuration, where a required image loader (e.g., JPEG loader) is missing or corrupted.
fix
This often points to a problem with the `sharp` installation's native components. Try completely reinstalling `sharp` by deleting `node_modules` and `package-lock.json` (or `yarn.lock`) and running `npm install sharp`. If installing on a custom environment or Docker, ensure all `libvips` dependencies are correctly set up for your specific build.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
46 hits · last 30 days
node
42
OpenAI (training)
1
Resources
sharp — npm install sharp · libregistry