#Run Sharp in Convex

4 messages · Page 1 of 1 (latest)

uncut pendant
#

I want to run sharp within a convex action so I can compress images I generate with AI before I put them into storage. This works locally, but when I deploy I get this error:

✖ Error fetching POST https://warmhearted-egret-307.convex.cloud/api/deploy2/start_push 400 Bad Request: InvalidModules: Hit an error while pushing:
Loading the pushed modules encountered the following
error:
Uncaught Failed to analyze imageCompressionService.js: Could not load the "sharp" module using the linux-arm64 runtime

What can I do?

dapper fieldBOT
#

Hi @uncut pendant. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the knowledge sources, I can help you with running Sharp in Convex actions.

The error you're encountering is related to bundling issues with the Sharp library. Sharp relies on native binaries (libvips) which can cause problems during the bundling process.

Here's what you need to do:

Solution: Use External Packages

Sharp is mentioned in the documentation as an example of a library with dynamic dependencies. You can resolve this by marking Sharp as an external package, which is a feature available for the Node.js runtime.

Steps to fix:

  1. Make sure your action file has the "use node" directive at the top
  2. Create or update a convex.json file in the same directory as your package.json
  3. Add Sharp to the external packages configuration:
{
  "node": {
    "externalPackages": ["sharp"]
  }
}

Alternatively, you can mark all dependencies as external:

{
  "node": {
    "externalPackages": ["*"]
  }
}

When you mark a package as external, it won't be bundled by esbuild. Instead, it will be installed from npm on the server the first time you push code that uses it, matching the version in your local node_modules folder.

Important notes:

  • External packages are only supported in the Node.js runtime, so ensure your action file starts with "use node";
  • After changing your package.json, run npm install to update your local node_modules before pushing to Convex
  • The total size of your source code bundle and external packages cannot exceed 45MB zipped or 240MB unzipped
#

This approach should resolve the bundling error and allow Sharp to work in your Convex actions.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: