Netlify "Failed to bundle function" - Fix Functions Build in CI
Netlify bundles each function with esbuild before deploy, and bundling failed - an import it cannot resolve, a missing dependency, or a native/binary module that does not bundle cleanly.
What this error means
The deploy fails in the functions-bundling step with an esbuild Could not resolve error or a bundling failure naming a function. It is deterministic: the same unresolved import or missing dependency fails every run until it is provided.
Failed to bundle function "api":
✘ [ERROR] Could not resolve "sharp"
netlify/functions/api.ts:3:18:
3 │ import sharp from "sharp";Common causes
Missing or unresolved dependency
The function imports a package that is not in package.json (or only a devDependency), so esbuild cannot resolve it when bundling for the function.
Native module that does not bundle
Packages with native binaries (sharp, bcrypt) cannot be inlined by esbuild and must be marked external so Netlify ships them as node modules instead.
How to fix it
Add the dependency and mark native modules external
Ensure the import is a real dependency, and tell the bundler to keep native modules external.
# netlify.toml
[functions]
node_bundler = "esbuild"
external_node_modules = ["sharp"]Resolve the import error
- Add the missing package to
dependencies(not justdevDependencies). - Fix the import path/specifier the error points at.
- Reproduce locally with
netlify buildto confirm bundling succeeds.
How to prevent it
- Keep function dependencies in
dependencies, notdevDependencies. - List native modules in
external_node_modules. - Run
netlify buildin PR CI to catch bundling errors early.