首页 > 解决方案 > Testing module that requires a module from a layer in AWS Lambda

问题描述

So, I am using Layers in my Serverless Framework Node JS project. I add all commonly used npm packages to a layer because I don't want every Lambda function to have huge packages.

The problem is that when running unit tests (locally), it can't find these modules.

Is there any way to handle this problem?

标签: node.jsaws-lambdajestjsserverless-frameworkaws-lambda-layers

解决方案


尝试以下操作:

  1. 将 package.json 文件添加到您的项目中,其中包含所有依赖项
  2. 安装依赖项,因此您将在本地拥有它们
  3. 在无服务器框架中使用Exclude选项,这样最终的包将不包含 node_modules 目录:
package:
  exclude:
    - node_modules/**

推荐阅读