Skip to content
Latchkey

AWS Lambda "Runtime.ImportModuleError: Cannot find module" in CI

The Lambda runtime tried to load your handler module before the first invocation and Node could not resolve it. The file named in the handler setting is not in the deployment package, or a dependency it imports was never bundled.

What this error means

A Lambda invoke in a CI integration test returns "Runtime.ImportModuleError: Error: Cannot find module 'X'" and the function never runs your code. The error appears in the init/load phase, before the handler is called.

lambda
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'handler'\nRequire stack:\n- /var/runtime/index.mjs",
  "trace": ["Runtime.ImportModuleError: Error: Cannot find module 'handler'"]
}

Common causes

The handler file is not in the package

The Handler setting names handler.handler, but the zip built in CI does not contain handler.js/handler.mjs at the package root (wrong build output path or a .gitignore/glob excluded it).

node_modules was not bundled

The handler imports a third-party package but node_modules (or the bundled output) was left out of the zip, so the require fails at load time.

How to fix it

Verify the handler path inside the artifact

  1. Unzip the built package and confirm the file named in Handler exists at the root.
  2. Check that Handler is file.exportedFunction with no directory prefix mismatch.
  3. Re-package so the entry file sits where the runtime looks for it.
Terminal
unzip -l function.zip | grep handler
# Handler = handler.handler  ->  needs handler.js at zip root

Bundle dependencies into the artifact

Run the install and any bundler before zipping so node_modules or the bundled output is included.

Terminal
npm ci --omit=dev
zip -r function.zip handler.js node_modules

How to prevent it

  • Assert the handler file exists in the artifact as a build step.
  • Bundle or vendor dependencies before zipping.
  • Keep the Handler setting in sync with the packaged file name.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →