You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用AWS CDK构建CodePipeline时遭遇TypeScript编译错误

解决AWS CDK Pipeline中TypeScript编译axios类型定义错误的问题

问题分析

TS1005、TS1135、TS1128这类错误通常源于axios版本与TypeScript版本的兼容性冲突,或是项目TS配置未正确排除第三方依赖类型文件,也可能是Lambda打包流程中的类型处理逻辑异常。

解决方案

1. 匹配axios与TypeScript的兼容版本

当前使用的axios@1.2.1与TS4.9.4存在兼容性问题,建议升级至稳定兼容版本。修改Lambda的package.json:

{
  "dependencies": {
    "@aws-sdk/client-sfn": "^3.226.0",
    "axios": "^1.6.0",
    "axios-retry": "^3.9.1"
  }
}

执行npm install更新依赖并生成新的package-lock.json。

2. 配置TS排除node_modules

确保根项目tsconfig.json中明确排除第三方依赖目录,避免编译时处理外部类型文件:

{
  "compilerOptions": {
    // 保留原有编译配置
  },
  "exclude": ["node_modules", "cdk.out"]
}

3. 使用项目本地TypeScript而非全局版本

Pipeline中全局安装TS可能与项目本地版本冲突,改用本地TS执行编译:

  • 在根项目执行npm i -D typescript@4.9.4,将TS添加到开发依赖
  • 修改CDK Pipeline的构建命令:
synth: new ShellStep("Deployment", {
  input: CodePipelineSource.codeCommit(this.codeRepository, environment, { codeBuildCloneOutput: true }),
  installCommands: ["npm i -g npm@latest"],
  commands: [
    "npx tsc -v", 
    "npm ci",
    "npm run build",
    "cdk synth",
  ],
})

4. 优化Lambda的NodejsFunction打包配置

让Lambda单独处理自身的TS编译,避免依赖根项目的编译流程:

this.intoMagentoQueueConsumer = new NodejsFunction(this, `intoMagentoQueueConsumer`, {
  runtime: Runtime.NODEJS_16_X,
  memorySize: 1024,
  handler: "handler",
  depsLockFilePath: join(__dirname, "../../src/lambda-handlers/queue-consumers/intoMagentoQueueConsumer/package-lock.json"),
  entry: join(__dirname, "../../src/lambda-handlers/queue-consumers/intoMagentoQueueConsumer/index.ts"),
  bundling: { 
    minify: false,
    nodeModules: ["@aws-sdk/client-sfn", "axios", "axios-retry"],
    externalModules: ["aws-sdk", "crypto-js"],
    tsconfig: join(__dirname, "../../src/lambda-handlers/queue-consumers/intoMagentoQueueConsumer/tsconfig.json"),
  },
  typescriptCheck: false, // 若无需类型检查可禁用
});

若Lambda目录无tsconfig.json,创建基础配置文件:

{
  "compilerOptions": {
    "target": "ES2020",
    "module": "commonjs",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": ["node_modules"]
}

5. 检查根项目build命令范围

确保根项目package.json中的build命令仅编译CDK栈代码,不包含Lambda目录。例如:

{
  "scripts": {
    "build": "tsc --project tsconfig.json"
  }
}

内容的提问来源于stack exchange,提问作者Luke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 11:25:25