Skip to content

Commit c2c71d9

Browse files
fix: better error when Sharp can't be resolved (ex: pnpm) (#8128)
* fix: better error when Sharp can't be resolved (ex: pnpm) * chore: changeset * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <[email protected]> --------- Co-authored-by: Sarah Rainsberger <[email protected]>
1 parent dbc97b1 commit c2c71d9

File tree

5 files changed

+47
-1
lines changed

5 files changed

+47
-1
lines changed

.changeset/cyan-carrots-stare.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Update error message when Sharp couldn't be found (tends to happen on pnpm notably)

packages/astro/config.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,10 @@ export function sharpImageService(): ImageServiceConfig;
2424
* Return the configuration needed to use the Squoosh-based image service
2525
*/
2626
export function squooshImageService(): ImageServiceConfig;
27+
28+
/**
29+
* Return the configuration needed to use the passthrough image service. This image services does not perform
30+
* any image transformations, and is mainly useful when your platform does not support other image services, or you are
31+
* not using Astro's built-in image processing.
32+
*/
33+
export function passthroughImageService(): ImageServiceConfig;

packages/astro/config.mjs

+7
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ export function squooshImageService() {
1313
config: {},
1414
};
1515
}
16+
17+
export function passthroughImageService() {
18+
return {
19+
entrypoint: 'astro/assets/services/noop',
20+
config: {},
21+
};
22+
}

packages/astro/src/assets/services/sharp.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { FormatEnum } from 'sharp';
2+
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
23
import type { ImageOutputFormat, ImageQualityPreset } from '../types.js';
34
import {
45
baseService,
@@ -21,7 +22,7 @@ async function loadSharp() {
2122
try {
2223
sharpImport = (await import('sharp')).default;
2324
} catch (e) {
24-
throw new Error('Could not find Sharp. Please install Sharp manually into your project.');
25+
throw new AstroError(AstroErrorData.MissingSharp);
2526
}
2627

2728
return sharpImport;

packages/astro/src/core/errors/errors-data.ts

+26
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,32 @@ export const InvalidDynamicRoute = {
791791
message: (route: string, invalidParam: string, received: string) =>
792792
`The ${invalidParam} param for route ${route} is invalid. Received **${received}**.`,
793793
} satisfies ErrorData;
794+
/**
795+
* @docs
796+
* @see
797+
* - [Default Image Service](https://docs.astro.build/en/guides/images/#default-image-service)
798+
* - [Image Component](https://docs.astro.build/en/guides/images/#image--astroassets)
799+
* - [Image Services API](https://docs.astro.build/en/reference/image-service-reference/)
800+
* @description
801+
* Sharp is the default image service used for `astro:assets`. When using a [strict package manager](https://pnpm.io/pnpm-vs-npm#npms-flat-tree) like pnpm, Sharp must be installed manually into your project in order to use image processing.
802+
*
803+
* If you are not using `astro:assets` for image processing, and do not wish to install Sharp, you can configure the following passthrough image service that does no processing:
804+
*
805+
* ```js
806+
* import { defineConfig, passthroughImageService } from "astro/config";
807+
* export default defineConfig({
808+
* image: {
809+
* service: passthroughImageService(),
810+
* },
811+
* });
812+
* ```
813+
*/
814+
export const MissingSharp = {
815+
name: 'MissingSharp',
816+
title: 'Could not find Sharp.',
817+
message: 'Could not find Sharp. Please install Sharp (`sharp`) manually into your project.',
818+
hint: "See Sharp's installation instructions for more information: https://sharp.pixelplumbing.com/install. If you are not relying on `astro:assets` to optimize, transform, or process any images, you can configure a passthrough image service instead of installing Sharp. See https://docs.astro.build/en/reference/errors/missing-sharp for more information.",
819+
};
794820
// No headings here, that way Vite errors are merged with Astro ones in the docs, which makes more sense to users.
795821
// Vite Errors - 4xxx
796822
/**

0 commit comments

Comments
 (0)