Skip to content
Latchkey

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.

Netlify deploy log
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
# netlify.toml
[functions]
  node_bundler = "esbuild"
  external_node_modules = ["sharp"]

Resolve the import error

  1. Add the missing package to dependencies (not just devDependencies).
  2. Fix the import path/specifier the error points at.
  3. Reproduce locally with netlify build to confirm bundling succeeds.

How to prevent it

  • Keep function dependencies in dependencies, not devDependencies.
  • List native modules in external_node_modules.
  • Run netlify build in PR CI to catch bundling errors early.

Frequently asked questions

What causes ""Failed to bundle function""?
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.
How do I fix "Failed to bundle function"?
Ensure the import is a real dependency, and tell the bundler to keep native modules external.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card