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

AWS Lambda Node.js环境导入@aws-sdk/client-ssm报语法错误如何解决

报错原因

Node.js 默认采用 CommonJS 模块规范,你代码中使用的 import 属于 ES Module(ESM)专属语法,当前 Lambda 运行环境没有将你的代码识别为 ESM 模块,因此抛出语法错误。

解决方案

你可以任选以下任意一种方案处理:

  • 方案一:改用 CommonJS 语法(推荐,适配 Lambda 默认运行配置)
    把 import 语句替换为 CommonJS 的 require 导入语法,修改后完整代码如下:
    const { SSMClient, GetParametersCommand } = require("@aws-sdk/client-ssm");
    const config = { region: "eu-central-1" };
    
    exports.handler = async (event, context) => {
      const client = new SSMClient({ region: config.region });
      const command = new GetParametersCommand({Names: ["/my-app/dev/db-url"]});
      const response = await client.send(command);
      console.log(response);
    };
    
    如果你是在Lambda控制台直接在线编辑代码,优先选这个方案,不需要额外配置其他文件。
  • 方案二:保留 ESM 语法,配置模块标识
    1. 在你的Lambda部署包根目录的package.json文件中添加配置:"type": "module"
    2. 同步修改代码的导出语法,把exports.handler改为ESM的导出写法,修改后完整代码如下:
    import { SSMClient, GetParametersCommand } from "@aws-sdk/client-ssm";
    const config = { region: "eu-central-1" };
    
    export const handler = async (event, context) => {
      const client = new SSMClient({ region: config.region });
      const command = new GetParametersCommand({Names: ["/my-app/dev/db-url"]});
      const response = await client.send(command);
      console.log(response);
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 11:48:02