Help adding Sharp to serverless function

I’m trying to get sharp into one of my serverless functions but getting an error the module is not found.

Error [ERR_MODULE_NOT_FOUND]: Cannot find package ‘sharp’ imported from /var/task/app/api/optimize-image.js

I’ve included the below into my vercel.json

{
  "installCommand": "npm install --arch=x64 --platform=linux sharp"
}

and below are my imports

import sharp from "sharp";
import fetch from "node-fetch";

Hi @arbyruns, sorry that you’re facing this issue. Can you please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

1 Like

Hey, Arby!

Definitely recommend sharing an example with us as @anshumanb mentioned, but also wanted to share similar errors that others have faced in the community marked as solved, in case this is helpful:

https://community.vercel.com/search?q=Error%20%5BERR_MODULE_NOT_FOUND%5D%20status:solved

Sorry all, it seems to be working now. I wonder if there was a delay on the server side because now it’s working properly. This was the error from the logs

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'sharp' imported from /var/task/app/api/optimize-image.js
    at packageResolve (node:internal/modules/esm/resolve:858:9)
    at moduleResolve (node:internal/modules/esm/resolve:931:18)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:1173:14)
    at defaultResolve (node:internal/modules/esm/resolve:1216:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:542:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:510:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:239:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:96:40)
    at link (node:internal/modules/esm/module_job:95:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}
Node.js process exited with exit status: 1. The logs above can help with debugging the issue.
2 Likes

Glad to hear it now works!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.