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

AWS Lambda中TypeScript模块导入报错:是否需打包及配置方法

AWS Lambda部署TypeScript代码模块找不到问题排查

问题背景

尝试将以下TypeScript代码部署到AWS Lambda(Runtime为Node.js 16.x,Handler为lambda.lambdaHandler),转译后测试时出现模块找不到错误。

TypeScript源码

import 'aws-sdk'
import { /* bunch of stuff... */ } from "@aws-sdk/client-cloudwatch-logs";
import {Context, APIGatewayProxyResult} from 'aws-lambda';
import {DateTime} from "luxon";

export const lambdaHandler = async (event: any, context: Context): Promise<APIGatewayProxyResult> => {
    /* ... stuff ... */
}

转译后的JavaScript代码

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lambdaHandler = void 0;
require("aws-sdk");
//import {CloudWatchClient} from "@aws-sdk/client-cloudwatch";
const client_cloudwatch_logs_1 = require("@aws-sdk/client-cloudwatch-logs");
const luxon_1 = require("luxon");
const lambdaHandler = async (event, context) => {
    /* ... transpiled stuff ... */
}

错误响应

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '@aws-sdk/client-cloudwatch-logs'\nRequire stack:\n- /var/task/lambda.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-cloudwatch-logs'",
    "Require stack:",
    "- /var/task/lambda.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:951:17)",
    "    at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:976:21)",
    "    at async start (file:///var/runtime/index.mjs:1137:23)",
    "    at async file:///var/runtime/index.mjs:1143:1"
  ]
}

当前tsconfig.json配置

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "Node",
    "target": "ES2022",
    "sourceMap": true,
    "lib": [
      "ES2021"
    ],
    "typeRoots": ["node_modules/@types"],
    "outDir": "build",
    "baseUrl": "src",
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "inlineSources": true,
    "rootDir": "src",
    "preserveConstEnums": true,
    "isolatedModules": true,
    "incremental": true,
    "importHelpers": true
  },
  "exclude": [
    "node_modules",
    "**/*.test.ts"
  ]
}

问题解答

1. @aws-sdk/client-cloudwatch-logs这类模块是否需要打包,还是Lambda运行时已内置?

Lambda Node.js 16.x运行时不会内置新的AWS SDK v3模块(比如@aws-sdk/client-cloudwatch-logs),仅内置旧版AWS SDK v2(即aws-sdk包)。所以这类v3模块必须随代码一起打包部署。

2. 若需要打包,应如何操作?是使用打包工具还是仅需配置tsconfig.json?

仅靠tsconfig.json无法完成第三方模块的打包——TypeScript编译器(tsc)只负责将TS转译为JS,不会把第三方依赖的代码合并到输出文件里。必须使用打包工具将你的代码和依赖模块打包成一个或多个包含所有必要代码的文件,再部署到Lambda。

3. 若无需打包工具,如何配置tsconfig.json以包含这些第三方模块?

不存在这种配置方式。tsc的核心功能是类型检查和转译,不具备依赖打包能力。如果不想用打包工具,只能手动将node_modules里的依赖模块复制到部署包中,和转译后的JS文件一起压缩上传。但这种方式容易出错(比如遗漏依赖、体积过大),不推荐。

4. 若必须使用打包工具,WebPack、Babel等是否都适用?

  • Webpack:完全适用,是Lambda TS项目最常用的打包工具之一。可以通过配置将代码和依赖打包成单个文件,还能做代码压缩、tree-shaking来减小包体积。
  • Babel:单独用Babel只能转译代码,无法处理依赖打包,需要配合@babel/preset-typescript和打包工具(比如Webpack、Rollup)一起使用。
  • 另外,也可以用esbuild——它是一个更快的TS转译+打包工具,对Lambda项目支持很好,配置简单,打包速度远快于Webpack。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 13:15:16