vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.1k stars 26.73k forks source link

How to use `Sharp` library in API with Vercel? #60409

Open rwecho opened 8 months ago

rwecho commented 8 months ago

Link to the code that reproduces this issue

https://github.com/rwecho/portal

To Reproduce

I reference the Sharp library by pnpm add sharp and enable the API to resize the image with it.

It's working well locally. But when I deployed it to the Vercel, the API returned with 500 status.

The detailed log:

image

Error: Could not load the "sharp" module using the linux-x64 runtime

I searched for the issue, and could not distinguish that the sharp library is build-in.

Current vs. Expected behavior

In the app, we can use the Image component to optimize the image, how to handle it in API? thanks

Verify canary release

Provide environment information

The vercel environment, how to get the vercel info?

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Which stage(s) are affected? (Select all that apply)

Vercel (Deployed)

Additional context

No response

Cow258 commented 8 months ago
// next.config.js
const nextConfig = {
  webpack: (cfg, options = {}) => {
    cfg.externals.push('sharp')
    const { webpack } = options
    const regex = /^sharp$/
    cfg.plugins.push(new webpack.IgnorePlugin({
      resourceRegExp: regex,
    }))
  }
}

p.s. sharp unable to run on Edge runtime, sharp only can run on serverless function

mwawrusch commented 1 month ago

Ran into this again today, with a working system (and using the workaround from above to no avail).