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.
{
"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
- Unzip the built package and confirm the file named in
Handlerexists at the root. - Check that
Handlerisfile.exportedFunctionwith no directory prefix mismatch. - Re-package so the entry file sits where the runtime looks for it.
unzip -l function.zip | grep handler
# Handler = handler.handler -> needs handler.js at zip rootBundle dependencies into the artifact
Run the install and any bundler before zipping so node_modules or the bundled output is included.
npm ci --omit=dev
zip -r function.zip handler.js node_modulesHow to prevent it
- Assert the handler file exists in the artifact as a build step.
- Bundle or vendor dependencies before zipping.
- Keep the
Handlersetting in sync with the packaged file name.