Runtime.ImportModuleError trying to access npm package in an AWS lambda function using layers
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Runtime.ImportModuleError in AWS Lambda often appears right after introducing layers for shared npm dependencies. The function deploys successfully, but runtime fails because Node.js cannot resolve the package path. This guide covers a working layer layout, deployment sequence, and a concise debugging checklist.
Understand How Node.js Layers Are Mounted
For Node.js functions, Lambda expects layer content under a top-level nodejs folder. Your dependencies should end up in nodejs/node_modules. If the archive root is wrong, Lambda mounts the layer but module resolution still fails.
Use this script to build a minimal layer package:
Publish the zip as a new layer version, then attach that version ARN to your Lambda function. Layer versions are immutable, so updates always require a new version.
Import Dependency in the Handler Correctly
Once the layer is attached, import by package name in your handler. Do not hardcode layer mount paths in application code.
If this still throws Runtime.ImportModuleError, the most likely issue is packaging shape or an outdated layer version on the function configuration.
Verify Runtime, Layer Version, and Build Environment
Debugging is faster when you validate one variable at a time. First confirm function runtime, then confirm attached layer ARNs, then confirm the layer content was built for that environment.
For pure JavaScript dependencies, local packaging often works. For native modules, build on Linux or in a compatible container so binaries match Lambda runtime expectations.
Production Workflow That Avoids Recurring Errors
A stable team workflow is: build layers in CI on Linux, publish through infrastructure code, and pin exact version ARNs in deployment manifests. Avoid manual console edits for layer attachment, because drift is hard to detect during incident response.
Keep one layer focused on shared runtime dependencies and avoid mixing unrelated utilities. Smaller, purpose-specific layers reduce blast radius and simplify rollbacks.
If multiple functions share the same layer, validate changes with a canary function before broad rollout. That protects unrelated services from accidental dependency upgrades.
Common Pitfalls
- Zipping the wrong folder level so
nodejsis not at archive root. - Publishing a new layer version but forgetting to update function configuration to the new ARN.
- Building native dependencies on macOS or Windows and deploying them directly to Lambda.
- Mixing package versions between function zip and layer, which creates resolution ambiguity.
- Changing runtime version without rebuilding and retesting shared layers.
Summary
- Most layer-based import errors come from archive layout and version attachment issues.
- Keep dependencies under
nodejs/node_modulesinside the layer zip. - Import by package name and let Lambda resolve module paths.
- Verify runtime, attached layer ARN, and published version alignment.
- Build and publish layers in a reproducible CI pipeline for predictable results.
- Keep layer packaging scripts under version control for auditability.
Related reading
- S3 - Access-Control-Allow-Origin Header
- S3 - What Exactly Is A Prefix? And what Ratelimits apply?
- S3 Bucket action doesn't apply to any resources
- S3 Bucket Lambda Event Unable to validate the following destination configurations
- RxJS Observable - Wait for async method to complete before using next emitted value
- RxJS Observable returns data immediately, why?
- S3 Bucket Policy to make a specific sub folder public and everything else private?
- S3 bucket policy vs access control list

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.