Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot build when using Sharp 0.33.0 #9345

Closed
1 task
Boston343 opened this issue Dec 6, 2023 · 29 comments · Fixed by #9653
Closed
1 task

Cannot build when using Sharp 0.33.0 #9345

Boston343 opened this issue Dec 6, 2023 · 29 comments · Fixed by #9653
Assignees
Labels
ecosystem: external External library doesn't work

Comments

@Boston343
Copy link

Boston343 commented Dec 6, 2023

Astro Info

Astro                    v4.0.2
Node                     v18.18.2
System                   Windows (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             auto-import
                         @astrojs/mdx
                         @astrojs/tailwind
                         @astrojs/sitemap
                         astro-compress

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

If I upgrade the Sharp dependency to 0.33.0 for image optimization with astro:assets, I am unable to build locally (npm run build). Development mode still works fine.

This happens with both Astro v3.6.4 and v4.0.2.

The error logged to the console looks like:

MissingSharp: Could not find Sharp. Please install Sharp (`sharp`) manually into your project or migrate to another image service.
    at loadSharp (file:///.../dist/chunks/astro/assets-service_x_nToqfa.mjs:352:11)
    at async Object.transform (file:///.../dist/chunks/astro/assets-service_x_nToqfa.mjs:364:15)
    at async generateImageInternal (file:///.../node_modules/astro/dist/assets/build/generate.js:119:24)
    at async generateImage (file:///.../node_modules/astro/dist/assets/build/generate.js:66:28)
    at async file:///.../node_modules/p-queue/dist/index.js:118:36 {
  loc: undefined,
  title: 'Could not find Sharp.',
  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.\n" +
    '\n' +
    'See https://docs.astro.build/en/guides/images/#default-image-service for more information on how to migrate to another image service.',
  frame: undefined,
  type: 'AstroError'
}

Additional notes: It seems to be an interaction between astro:assets and astro-compress. If I even have the line import compress from "astro-compress" inside astro.config.mjs the build will fail (I don't even have to add it to the integrations arrray). However, if I remove the compress() integration, and comment out the import statement, it will build fine. THEN I can add the astro-compress import and integration back into the integrations array and it will build and compress fine. But if I then go and add an image or otherwise cause the images to need re-generated (since they aren't cached), the issue will occur again. And then you'll have to do the comment out compress and what-not all over again.

If I downgrade sharp back to 0.32.6 it builds just fine.

What's the expected result?

The website should build and this error should not be thrown.

Link to Minimal Reproducible Example

Stackblitz link. You need to download it, run npm install, then npm run build to see the error. See the "Additional notes" section above for more details.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 6, 2023
@Princesseuh
Copy link
Member

Could you provide a reproduction? I'm building many websites using Sharp 0.33.0 and Astro 4 successfully both locally and in CI with no issues.

@Princesseuh Princesseuh added needs repro Issue needs a reproduction and removed needs triage Issue needs to be triaged labels Dec 6, 2023
Copy link
Contributor

github-actions bot commented Dec 6, 2023

Hello @Boston343. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@Boston343
Copy link
Author

Could you provide a reproduction? I'm building many websites using Sharp 0.33.0 and Astro 4 successfully both locally and in CI with no issues.

Working on trying to replicate on Stackblitz and haven't been able to yet. Might try a local copy of the same code to see if anything changes.

@Boston343
Copy link
Author

Boston343 commented Dec 6, 2023

@Princesseuh I added a stackblitz link, although you'll need to download it to your machine and install npm packages, then attempt to build. I added an additional comment which I've copied here. I'm not sure if this is an astro problem or an astro-compress problem but it seems to be an interaction between the two.

Additional notes: It seems to be an interaction between astro:assets and astro-compress. If I even have the line import compress from "astro-compress" inside astro.config.mjs the build will fail (I don't even have to add it to the integrations arrray). However, if I remove the compress() integration, and comment out the import statement, it will build fine. THEN I can add the astro-compress import and integration back into the integrations array and it will build and compress fine. But if I then go and add an image or otherwise cause the images to need re-generated (since they aren't cached), the issue will occur again. And then you'll have to do the comment out compress and what-not all over again.

But again, if I downgrade sharp to 0.32.6, this issue doesn't happen. So maybe it's a sharp issue? I really have no idea how to diagnose further unfortunately.

@natemoo-re natemoo-re added ecosystem: external External library doesn't work and removed needs repro Issue needs a reproduction labels Dec 7, 2023
@josuazurbruegg
Copy link

I'm having trouble building using the most recent sharp version too. Downgrading sharp to 0.32.6 helped me as well.
I'm using Astro 4.0.3. I'm not using the astro-compress library. My project is pretty basic but I'm generating various dynamic images using the getImage server-side helper, which is the only thing slightly out of the ordinary that I'm doing.

Happy to provide a reproducible example if needed still!

P.S. this is my first project working with Astro and it has been an absolute treat! A big thank you to everyone involved in the work on the project!

@Princesseuh Princesseuh self-assigned this Dec 11, 2023
@Princesseuh
Copy link
Member

Hmm, following the reproduction steps, everything seems to work just fine for me.

One thing I do notice with the astro-compress stuff, is that it pulls an old version of Sharp in addition to the installed one, I wonder if there could be an incompatibility due to having multiple versions installed, perhaps. Nonetheless, I'm not able to reproduce the error in the OP 😦

@notjosh
Copy link

notjosh commented Dec 13, 2023

When I dump the actual error, I get the following exception:

Error: Could not load the "sharp" module using the darwin-x64 runtime
Possible solutions:
- Add platform-specific dependencies:
    npm install --os=darwin --cpu=x64 sharp
  or
    npm install --force @img/sharp-darwin-x64

Turns out, Sharp 0.33.0 changed how binaries are distributed (tldr: lovell/sharp#3750 (comment)).

The fix for me was to run yarn add sharp --ignore-engines, which seems to persist even when node_modules is removed. It's weird though, because I already had sharp in package.json, so I'd expect it to have worked. Computers 🤷 (edit: maybe I spoke too soon on that last point!)

@fflaten
Copy link
Contributor

fflaten commented Dec 18, 2023

Had the same error in OP since 0.33.0 was released. Remember to clear asset cache between changing versions to force error.

Using pnpm on Debian 11 (devcontainer). Can recreate using a new portfolio template project by only changing portrait.jpg on index page to use <Image> and installing 0.33.1 sharp as dependency. Assuming issue was because Astro has peer dep on 0.32.*

@ppo
Copy link

ppo commented Dec 23, 2023

I had the same problem installing my site on my server using @astrojs/node, using Astro 4.0.3.

Installing "sharp": "^0.32" on the server (with node v20.10.0) solved the problem.
On my computer (with node v21.2.0), "sharp": "^0.33.1" works fine.

@fflaten
Copy link
Contributor

fflaten commented Dec 27, 2023

@Princesseuh 4.0.8/#9463 didn't resolve it unfortunately.

All deps using same version yet it fails 😞

pnpm why sharp
Legend: production dependency, optional only, dev only

@fflaten/website@0.0.1 /workspaces/Website

dependencies:
@astrojs/mdx 2.0.3
└─┬ astro 4.0.8 peer
  └── sharp 0.33.1
astro 4.0.8
└── sharp 0.33.1
astro-expressive-code 0.30.1
└─┬ astro 4.0.8 peer
  └── sharp 0.33.1
sharp 0.33.1

devDependencies:
astro-auto-import 0.4.2
└─┬ astro 4.0.8 peer
  └── sharp 0.33.1
 generating optimized images 
[CouldNotTransformImage] Could not transform image `/_astro/me.6Zkk9p1x.png`. See the stack trace for more information.
  Hint:
    This is often caused by a corrupted or malformed image. Re-exporting the image from your image editor may fix this issue.
  Error reference:
    https://docs.astro.build/en/reference/errors/could-not-transform-image/
  Stack trace:
    at generateImageInternal (file:///workspaces/Website/node_modules/.pnpm/astro@4.0.8_@types+node@20.10.5_sass@1.69.5/node_modules/astro/dist/assets/build/generate.js:129:21)
    at async file:///workspaces/Website/node_modules/.pnpm/p-queue@8.0.1/node_modules/p-queue/dist/index.js:187:36
  Caused by:
  Could not find Sharp. Please install Sharp (`sharp`) manually into your project or migrate to another image service.
    at loadSharp (file:///workspaces/Website/dist/chunks/astro/assets-service_YPMpKCGM.mjs:318:11)
    at async generateImageInternal (file:///workspaces/Website/node_modules/.pnpm/astro@4.0.8_@types+node@20.10.5_sass@1.69.5/node_modules/astro/dist/assets/build/generate.js:123:26)
    at async file:///workspaces/Website/node_modules/.pnpm/p-queue@8.0.1/node_modules/p-queue/dist/index.js:187:36

@Princesseuh
Copy link
Member

Hmm, that's annoying. I unfortunately do not know, and I still cannot reproduce any issues myself.

A question: What version of Node are you using?

@fflaten
Copy link
Contributor

fflaten commented Dec 27, 2023

v18.16.0

@Princesseuh
Copy link
Member

Ah, then I know the issue, Sharp's binaries lowest supported version is ^18.17.0. Try upgrading to at least 18.17.0 (the full semver check is ^18.17.0 || ^20.3.0 || >=21.0.0)

That's unfortunate, wonder why it's that version specifically, we might have to pin until the next major of Astro... so annoying.

@fflaten
Copy link
Contributor

fflaten commented Dec 27, 2023

I'm new to JS/TS world, but shouldn't it complain during pnpm install about that while installing sharp? 😕
Either way I still get the same error with v18.19.0. Trying to reproduce in a new repo with devcontainer/codespace now.

@Princesseuh
Copy link
Member

Princesseuh commented Dec 27, 2023

I'm new to JS/TS world, but shouldn't it complain during pnpm install about that while installing sharp? 😕 Either way I still get the same error with v18.19.0. Trying to reproduce in a new repo with devcontainer/codespace now.

No, because those are all optionalDeps, the JS world doesn't handle dependencies like this super well, unfortunately...

@fflaten
Copy link
Contributor

fflaten commented Dec 27, 2023

Either way I still get the same error with v18.19.0. Trying to reproduce in a new repo with devcontainer/codespace now.

Scratch that. Forgot to wipe node_modules after changing versions. v18.17.0+ works - and reproducable in codespace with 18.16.0 🥳 Sorry for not testing that. Haven't touched node version since last Astro requirement change.

@Boston343 You mentioned v18.18.2 in OP. Have you deleted node_modules since moving to that node version? If not, could you try it and install deps again?

Update

I'm new to JS/TS world, but shouldn't it complain during pnpm install about that while installing sharp? 😕

No, because those are all optionalDeps, the JS world doesn't handle dependencies like this super well, unfortunately...

sharp is non-optional dependency in my project, so would expect it to catch it there. It fails as expected by using pnpm install --engine-strict so I've added engine-strict=true to .npmrc now avoid this again 🙂

@PaulMorris
Copy link

PaulMorris commented Dec 28, 2023

I was getting a build failure on Cloudflare pages: [ERROR] [vite] [vite]: Rollup failed to resolve import "sharp" while using nodejs 18.14.1. I found this issue, upgraded to nodejs 20.10.0 and that resolved the build failure.

@LouisJS
Copy link

LouisJS commented Dec 28, 2023

Astro v4.0.8
Node v20.10.0
System macOS (x64)
Package Manager npm
Output server
Adapter @astrojs/vercel/serverless
Integrations @astrojs/react
@astrojs/tailwind

Using npm run dev in development works. But as soon as i host it on Vercel : https://louis-lecocq.vercel.app/

@Boston343
Copy link
Author

@fflaten yeah I've deleted node_modules, along with dist, and .astro folders at various points and it doesn't seem to help.

Currently running Node v20.10.0 and npm 10.2.3, otherwise same stuff as original post

I'm getting intermittent errors now with Sharp even while in dev mode with Astro 4.0.8 where it says "Could not find Sharp.", when it could just 2 minutes ago, without me even closing the dev environment (just updating a file so it reloads). I have not managed to find consistency with what causes it. Right now I'm working with 4.0.7 and it seems fine. Seems like Sharp ^0.33.0 is just causing problems.

@emrerdem1
Copy link

emrerdem1 commented Jan 4, 2024

This issue only occurs after migrating to v4.1.0 for me.

Node: v20.10.0
Astro: 4.1.0
Yarn: 1.22.19

I'm getting these warnings about Sharp after nuking and re-installing node_modules:

warning package.json: No license field
warning landing-page@0.0.1: No license field
[1/4] Resolving packages...
[2/4] Fetching packages...
warning sharp@0.33.1: The engine "libvips" appears to
warning @img/sharp-darwin-arm64@0.33.1: The engine "p
warning @img/sharp-darwin-arm64@0.33.1: The engine "g
warning @img/sharp-darwin-x64@0.33.1: The engine "pnp
warning @img/sharp-darwin-x64@0.33.1: The engine "gli
warning @img/sharp-libvips-darwin-arm64@1.0.0: The en
warning @img/sharp-libvips-darwin-arm64@1.0.0: The en
warning @img/sharp-libvips-darwin-x64@1.0.0: The engi
warning @img/sharp-libvips-darwin-x64@1.0.0: The engi
warning @img/sharp-libvips-linux-arm@1.0.0: The engin
warning @img/sharp-libvips-linux-arm@1.0.0: The engin
warning @img/sharp-libvips-linux-arm64@1.0.0: The eng
warning @img/sharp-libvips-linux-arm64@1.0.0: The eng
warning @img/sharp-libvips-linux-s390x@1.0.0: The eng
warning @img/sharp-libvips-linux-s390x@1.0.0: The eng
warning @img/sharp-libvips-linux-x64@1.0.0: The engin
warning @img/sharp-libvips-linux-x64@1.0.0: The engin
warning @img/sharp-libvips-linuxmusl-arm64@1.0.0: The
warning @img/sharp-libvips-linuxmusl-arm64@1.0.0: The
warning @img/sharp-libvips-linuxmusl-x64@1.0.0: The e
warning @img/sharp-libvips-linuxmusl-x64@1.0.0: The e
warning @img/sharp-linux-arm@0.33.1: The engine "pnpm
warning @img/sharp-linux-arm@0.33.1: The engine "glib
warning @img/sharp-linux-arm64@0.33.1: The engine "pn
warning @img/sharp-linux-arm64@0.33.1: The engine "gl
ine "musl" appears to be invalid.
warning @img/sharp-wasm32@0.33.1: The engine "pnpm" appears to be invalid.
warning @img/sharp-win32-ia32@0.33.1: The engine "pnpm" appears to be invalid.
warning @img/sharp-win32-x64@0.33.1: The engine "pnpm" appears to be invalid.
[3/4] Linking dependencies...
warning "@astrojs/react > @vitejs/plugin-react@4.2.1" has unmet peer dependency "vite@^4.2.0 || ^5.0.0".
warning Workspaces can only be enabled in private projects.
[4/4] Building fresh packages...

Error:

Could not process image request: MissingSharp: Could not find Sharp. Please install Sharp (`sharp`) manually into your project or migrate to another image service.
    at loadSharp (C:/Users/A/project/node_modules/astro/dist/assets/services/sharp.js:19:11)
    at async Object.transform (C:/Users/A/project/node_modules/astro/dist/assets/services/sharp.js:31:15) 
    at async Module.GET (C:/Users/A/project/node_modules/astro/dist/assets/endpoint/node.js:67:30)        
    at async callEndpoint (file:///C:/A/A/project/node_modules/astro/dist/core/endpoint/index.js:124:16)
    at async #tryRenderRoute (file:///C:/Users/A/project/node_modules/astro/dist/core/pipeline.js:119:16) 
    at async DevPipeline.renderRoute (file:///C:/Users/A/project/node_modules/astro/dist/core/pipeline.js:58:20)
    at async handleRoute (file:///C:/Users/A/project/node_modules/astro/dist/vite-plugin-astro-server/route.js:248:18)
    at async run (file:///C:/Users/A/project/node_modules/astro/dist/vite-plugin-astro-server/request.js:50:14)
    at async runWithErrorHandling (file:///C:/Users/A/project/node_modules/astro/dist/vite-plugin-astro-server/controller.js:64:5)
    at async handleRequest (file:///C:/Users/A/project/node_modules/astro/dist/vite-plugin-astro-server/request.js:44:3) {
  loc: undefined,
  title: 'Could not find Sharp.',
  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.\n" +
    '\n' +
    'See https://docs.astro.build/en/guides/images/#default-image-service for more information on how to migrate to another image service.',
  frame: undefined,
  type: 'AstroError'
}
22:49:22 [500] /_image 130ms

Sharp dep is getting installed just fine in v4.0.6, so I had to downgrade back to the working version.

@Princesseuh
Copy link
Member

4.1.0 includes no changes to this, so it's most likely a coincidence!

@emrerdem1
Copy link

4.1.0 includes no changes to this, so it's most likely a coincidence!

It could be. I tried upgrading to 4.1.0 twice, and had to downgrade back to 4.0.6. I don't know what's the cause, unfortunately.

@vrabe
Copy link

vrabe commented Jan 7, 2024

It could be. I tried upgrading to 4.1.0 twice, and had to downgrade back to 4.0.6. I don't know what's the cause, unfortunately.

Downgrading to 4.0.6 works for me though warnings still appear. I don't know why either.

@silveltman
Copy link

The fix for me was to run yarn add sharp --ignore-engines, which seems to persist even when node_modules is removed. It's weird though, because I already had sharp in package.json, so I'd expect it to have worked. Computers 🤷 (edit: maybe I spoke too soon on that last point!)

yarn add sharp --ignore-engines also fixed it for me

@AdamKniec
Copy link

AdamKniec commented Jan 7, 2024

I had a problem to render any image on Astro v4.0.8 using the built in <Image> component... I was about to give up but luckily I found this thread.

If you are getting errors / warnings about missing Sharp or images beeing undefined even though the path you give to src is correct then try

yarn add sharp --ignore-engines just like guys above suggested

I don't know if it's the proper way to fix that but I'm finally able to render the image...

Thanks you good people on this thread :D

@ohrrkan ohrrkan mentioned this issue Jan 8, 2024
1 task
@nimser
Copy link

nimser commented Jan 9, 2024

For me all tips here weren't enough, I had to update pnpm itself with pnpm i --global pnpm (as well as setting a recent node version)

@joao-pedrozo
Copy link

For me all tips here weren't enough, I had to update pnpm itself with pnpm i --global pnpm (as well as setting a recent node version)

Thanks

@jacklenox
Copy link
Contributor

Just to confirm that for me as well, I just needed to update my versions of node and npm. I use nvm, so it was a matter of: nvm install --lts

@nemanjam
Copy link

I had the same problem installing my site on my server using @astrojs/node, using Astro 4.0.3.

Installing "sharp": "^0.32" on the server (with node v20.10.0) solved the problem. On my computer (with node v21.2.0), "sharp": "^0.33.1" works fine.

0.32.6 works, while 0.33.3 fails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ecosystem: external External library doesn't work
Projects
None yet
Development

Successfully merging a pull request may close this issue.