首页 > 解决方案 > 如何使用 @aws-cdk/pipelines 模块构造打包依赖项?出现错误 TS2307

问题描述

我一直在尝试使用新的 AWS CDK 管道构造 ( https://docs.aws.amazon.com/cdk/api/latest/docs/pipelines-readme.html ),并成功设置了几个项目,包括关注此处的博客文章公告:https ://aws.amazon.com/blogs/developer/cdk-pipelines-continuous-delivery-for-aws-cdk-applications/ 。

但是,一旦我尝试向我的 lambdas 添加依赖项,构建就会失败并显示

lib/lambda/handler.ts(2,24): error TS2307: Cannot find module 'stripe' or its corresponding type declarations.

我已经在 lambdas 目录中安装了 package.json 文件和 node_modules,尝试压缩 lambdas 和 node_modules,尝试使用控制台上传 zip 文件,并尝试在“synthAction”步骤中获取“buildCommand”以安装依赖关系。没有任何效果。

该资产似乎是在 cdk.out 目录中创建的,并且正在上传代码更改,但 node_modules 本身从未与 lambda 函数一起打包。

我正在使用“SimpleSynthAction.standardNpmSynth”操作,以及“buildCommand”步骤中的“npm run build”命令。

我查看了Lambda 在使用 CDK 部署时无法从外部文件夹中找到模块,如何在使用 AWS CDK和https://github.com/aws-samples/aws-cdk-examples构建 cdk 时安装 lambda 函数的依赖项/issues/110#issuecomment-550506116没有运气。

我错过了什么?

标签: aws-lambdaaws-cdk

解决方案


Looks like the issue has to do with bundling. I abandoned the 'all-in on CDK approach' and went back to using a SAM/CDK hybrid, but this new blog post suggests bundling is the answer.

https://aws.amazon.com/blogs/devops/building-apps-with-aws-cdk/

Specifically, it references this construct, which likely would have helped in my situation: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html

Something to try in future.


推荐阅读