Serverless Framework with AWS Lambda error Cannot find module
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
The "Cannot find module" error in AWS Lambda functions deployed using the Serverless Framework is a common issue that developers might encounter. This error typically indicates that the function cannot locate a required module, either because it is not included in the deployment package or due to a misconfiguration in the module path. Below, we delve into the possible causes, solutions, and best practices to resolve this error.
Understanding the Error
AWS Lambda functions are executed inside a container, and they require all dependent modules to be present within the deployment package. When the error "Cannot find module" occurs, it usually points to:
- Missing Dependencies: The module is not included in the deployment package.
- Incorrect Path: The path to the module is incorrectly specified.
- Immutable Layers: Changes in the function's execution environment are needed, such as additional layers.
- Configuration Issues: Mistakes within the `serverless.yml` file or environment settings.
Common Scenarios and Solutions
Scenario 1: Missing Dependencies
Symptom: The function cannot find a module that is listed as a dependency.
Solution:
- Ensure the module is listed correctly in your `package.json` file and is correctly installed.
- Use the `serverless package` command to verify the contents of the deployed package and ensure all dependencies are included.
- For Node.js projects, avoid using `.npmignore` files that exclude necessary modules.
- Double-check the import statements for errors. Ensure that relative paths are correctly specified.
- Use absolute paths if necessary, and configure the `baseUrl` or `paths` properties in `tsconfig.json` for TypeScript projects.
- Define the layer in the `serverless.yml` file.
- Ensure the ARN for the layer is correct and compatible with the Lambda runtime.
- arn:aws:lambda:``<REGION>``:``<ACCOUNT_ID>``:layer:``<LAYER_NAME>``:``<VERSION>``
- Ensure `serverless.yml` is correctly configured, particularly the `package` section.
- Use the `external` configuration for large or specific dependencies.
- node_modules/**
- node_modules/specificModule/**
- Deployment Size: Keep your deployment packages small. Use tools like Webpack or Browserify to bundle only necessary code.
- Environment Consistency: Ensure your local development and AWS execution environments are consistent.
- Layer Utilization: Use Lambda layers wisely, especially for large dependencies like `aws-sdk`, to prevent deployment package bloat.
Related reading
- Service account secret is not listed. How to fix it?
- Service discovery vs load balancing
- Service located in another namespace
- Service Meshes like Istio vs. Event-Driven architecture for Microservices
- Service is killed in sleep mode.Why?
- Service Reference Error Failed to generate code for the service reference
- Setting http response header from AWS lambda
- Setting the capability for aws cloudformation template-validate

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.