Skip to main content

sharp

PropertyValue
Packagesharp
Versions Covered>=0.30.0 <1.0.0
Contract Version1.0.0
Statusproduction
Last Verified2026-02-27
Maintainercorpus-team

Installation

npm install sharp

Covered Functions

This contract covers 2 function(s):

toFile()

Writes processed image to file system

Import:

import { toFile } from 'sharp';

Postconditions

What happens after calling this function:

🔴 ERROR - tofile-rejects-on-error

Condition: file system error, invalid image data, or processing failure

Throws: Promise rejection with Error (ENOENT, EACCES, ENOMEM, or processing errors)

Required Handling:

Caller MUST use try-catch or .catch() to handle Promise rejections from toFile(). File system errors (missing directory, permissions), invalid image data, and memory issues will reject the Promise and crash the application if unhandled. Use pattern: try await sharp(input).toFile('output.jpg'); catch (error) /* handle */

📖 Source


toBuffer()

Returns processed image as Buffer

Import:

import { toBuffer } from 'sharp';

Postconditions

What happens after calling this function:

🔴 ERROR - tobuffer-rejects-on-error

Condition: invalid image data or processing failure

Throws: Promise rejection with Error

Required Handling:

Caller MUST use try-catch or .catch() to handle Promise rejections from toBuffer(). Invalid or corrupted image data will reject the Promise and crash if unhandled. Use pattern: try const buffer = await sharp(input).toBuffer(); catch (error) /* handle */

📖 Source


Example: Proper Error Handling

import sharp from 'sharp';

async function example() {
try {
const result = await toFile(/* args */);
// Handle success
return result;
} catch (error) {
// Handle error according to contract postconditions
console.error('Error:', error);
throw error;
}
}

See Also