AWS Lambda
npm package
ImportModuleError
AWS Lambda layers
Node.js

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.

Practice system design

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:

bash
1set -euo pipefail
2
3WORKDIR="$(pwd)/layer-demo"
4rm -rf "$WORKDIR"
5mkdir -p "$WORKDIR/layer/nodejs"
6cd "$WORKDIR/layer/nodejs"
7
8npm init -y
9npm install axios@1
10
11cd "$WORKDIR/layer"
12zip -r ../axios-layer.zip nodejs
13
14echo "Layer package created at $WORKDIR/axios-layer.zip"

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.

javascript
1import axios from "axios";
2
3export const handler = async () => {
4  const response = await axios.get("https://httpbin.org/get", {
5    timeout: 3000,
6  });
7
8  return {
9    statusCode: 200,
10    body: JSON.stringify({
11      ok: true,
12      url: response.data.url,
13    }),
14  };
15};

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.

bash
1# Check Lambda runtime
2aws lambda get-function-configuration \
3  --function-name my-fn \
4  --query 'Runtime'
5
6# Check attached layer ARNs
7aws lambda get-function-configuration \
8  --function-name my-fn \
9  --query 'Layers[*].Arn'
10
11# List versions for your layer name
12aws lambda list-layer-versions \
13  --layer-name axios-shared-layer

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 nodejs is 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_modules inside 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.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design