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

AWS CDK Pipeline构建遇TS1192错误:模块无默认导出求助

解决AWS CDK Pipeline构建Lambda时TS1192错误及cdk.out上传失败问题

问题现象

使用AWS CDK搭建Pipeline,通过NodejsFunction创建Lambda栈时,构建阶段执行npm run build触发TS1192错误,后续cdk.out上传环节失败。错误日志如下:

found 0 vulnerabilities

[Container] 2023/11/01 09:50:54.339059 Running command npm run build

> php_call@0.1.0 build
> tsc

node_modules/@elastic/transport/lib/connection/HttpConnection.d.ts(3,8): error TS1192: Module '/codebuild/output/src1897116248/src/node_modules/hpagent/index' has no default export.
node_modules/@elastic/transport/lib/connection/HttpConnection.d.ts(4,8): error TS1192: Module "http" has no default export.
node_modules/@elastic/transport/lib/connection/HttpConnection.d.ts(5,8): error TS1192: Module "https" has no default export.

[Container] 2023/11/01 09:51:08.403663 Command did not exit successfully npm run build exit status 2
[Container] 2023/11/01 09:51:08.407098 Phase complete: BUILD State: FAILED
[Container] 2023/11/01 09:51:08.407124 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm run build. Reason: exit status 2
[Container] 2023/11/01 09:51:08.453719 Entering phase POST_BUILD
[Container] 2023/11/01 09:51:08.455394 Phase complete: POST_BUILD State: SUCCEEDED
[Container] 2023/11/01 09:51:08.455406 Phase context status code:  Message: 
[Container] 2023/11/01 09:51:08.560643 Expanding base directory path: cdk.out
[Container] 2023/11/01 09:51:08.562579 Assembling file list
[Container] 2023/11/01 09:51:08.562592 Expanding cdk.out
[Container] 2023/11/01 09:51:08.564242 Skipping invalid file path cdk.out
[Container] 2023/11/01 09:51:08.564291 Phase complete: UPLOAD_ARTIFACTS State: FAILED
[Container] 2023/11/01 09:51:08.564300 Phase context status code: CLIENT_ERROR Message: no matching base directory path found for cdk.out

Lambda栈代码:

const db = new NodejsFunction(this, 'phpcall', {
  bundling: {
    nodeModules: ['axios', 'crypto', '@elastic/elasticsearch']
  }
})

Lambda代码导入语句:

import { Client } from '@elastic/elasticsearch'

错误原因

  1. TypeScript配置问题:http、https是Node.js原生CommonJS模块,hpagent同样为CommonJS模块,当前TypeScript配置未启用对CommonJS模块默认导入的兼容处理,导致类型检查报错。
  2. cdk.out上传失败是连锁问题:构建阶段失败导致CDK未生成cdk.out目录,后续上传环节自然找不到该路径。

解决步骤

1. 调整TypeScript配置(tsconfig.json)

修改项目根目录下的tsconfig.json,启用CommonJS模块兼容选项:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "target": "ES2020",
    "module": "ES2020"
    // 保留其他原有配置
  }
}
  • esModuleInterop:允许TypeScript将CommonJS模块当作ES模块处理,自动生成默认导入。
  • allowSyntheticDefaultImports:允许使用import X from 'module'语法导入无默认导出的模块。
  • moduleResolution:使用Node.js的模块解析规则。

2. 优化NodejsFunction的Bundling配置

确保依赖模块完整,并指定正确的Node.js运行时参数:

const db = new NodejsFunction(this, 'phpcall', {
  runtime: Runtime.NODEJS_18_X, // 匹配Lambda运行时版本
  bundling: {
    nodeModules: ['axios', '@elastic/elasticsearch', '@elastic/transport'], // 添加@elastic/transport依赖
    esbuildOptions: {
      platform: 'node', // 指定构建目标为Node.js环境
      target: 'node18' // 与runtime版本一致
    }
  }
})
  • 添加@elastic/transport到nodeModules:@elastic/elasticsearch依赖该模块,明确指定后避免bundler对其类型处理出错。
  • 指定platform和target:确保esbuild按Node.js环境构建,兼容原生模块。

3. 验证依赖版本(可选)

若上述调整后仍报错,可尝试升级或降级@elastic/elasticsearch到稳定版本(如8.11.0),避免版本间类型定义的兼容性问题:

npm install @elastic/elasticsearch@8.11.0

验证

重新触发Pipeline构建,确认npm run build执行成功,cdk.out目录正常生成并上传。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 06:24:57