AWS SAM "Build Failed" - Fix sam build Errors in CI
sam build compiles each function’s dependencies before packaging, and one function’s build step failed. The error is in the per-function build - a missing runtime, container, or a bad dependency install.
What this error means
sam build stops with Build Failed and a per-function reason: a missing language runtime, no Docker for --use-container, or a pip/npm install error inside the build. Deploy never runs because the artifacts were not produced.
Build Failed
Error: NodejsNpmBuilder:NpmInstall - NPM Failed: npm ERR! code ENOENT
npm ERR! Could not read package.jsonCommon causes
Missing runtime or container for the build
SAM builds with the function’s runtime, or in a Lambda-like container when --use-container is set. If that runtime is absent (no Node/Python) or Docker is unavailable, the build fails.
Dependency manifest or install error
A missing or malformed package.json/requirements.txt, or a dependency that fails to install, makes the per-function builder exit non-zero.
How to fix it
Build in a container for parity
Use --use-container so the build runs in the Lambda runtime image regardless of what the runner has installed (requires Docker on the runner).
sam build --use-container --cachedFix the function’s build inputs
- Confirm the manifest (
package.json,requirements.txt) exists at the function’sCodeUri. - Install or pin the matching runtime in CI (e.g.
actions/setup-nodefor a Node function). - Reproduce locally with the same command to read the full builder error.
How to prevent it
- Use
--use-containerin CI so builds match the Lambda runtime exactly. - Set up the function runtime explicitly in the workflow.
- Cache the SAM build cache keyed on the dependency manifest.