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

AWS Lambda用@azure/identity遇ClientSecretCredential非构造函数错误

解决AWS Lambda中ClientSecretCredential is not a constructor错误

错误根源

你代码里的核心问题是动态import()的使用方式错误:import("@azure/identity")返回的是一个Promise对象,而非直接的模块导出内容,直接解构得到的ClientSecretCredential是Promise,不是可实例化的构造函数,因此触发该错误。


修复方案

1. 修正导入方式

根据你的Lambda文件后缀.mjs(ESM模块),有两种修复方式:

方式一:使用静态import(推荐)

直接在文件顶部用静态import导入模块,这是ESM的标准写法:

import { ClientSecretCredential } from "@azure/identity";
import { GraphRbacManagementClient } from "@azure/graph";

export const handler = async (event, context) => {
  try {
    // 原有逻辑不变
    const tenantId = process.env.TENANT_ID || 'YOUR_TENANT_ID';
    const clientId = process.env.CLIENT_ID || 'YOUR_CLIENT_ID';
    const clientSecret = process.env.CLIENT_SECRET || 'YOUR_CLIENT_SECRET';

    const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    const graphClient = new GraphRbacManagementClient(credential, tenantId);

    const userId = process.env.USER_ID || 'USER_OBJECT_ID';
    const updatedUserProperties = {
      accountEnabled: true,
      // 添加其他需要更新的属性
    };

    const updatedUser = await graphClient.users.update(userId, updatedUserProperties);

    console.log('用户更新成功:', updatedUser);
    
    return {
      statusCode: 200,
      body: JSON.stringify({ message: '用户更新成功' }),
    };
  } catch (error) {
    console.error('错误:', error);

    return {
      statusCode: 500,
      body: JSON.stringify({ message: '内部服务器错误' }),
    };
  }
};
方式二:如果必须用动态import,需添加await

若因特殊需求要动态导入,必须在import()前加await,确保模块加载完成后再解构:

export const handler = async (event, context) => {
  try {
    // 先await加载模块
    const { ClientSecretCredential } = await import("@azure/identity");
    const { GraphRbacManagementClient } = await import("@azure/graph");

    // 后续逻辑不变
    const tenantId = process.env.TENANT_ID || 'YOUR_TENANT_ID';
    const clientId = process.env.CLIENT_ID || 'YOUR_CLIENT_ID';
    const clientSecret = process.env.CLIENT_SECRET || 'YOUR_CLIENT_SECRET';

    const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
    const graphClient = new GraphRbacManagementClient(credential, tenantId);

    const userId = process.env.USER_ID || 'USER_OBJECT_ID';
    const updatedUserProperties = {
      accountEnabled: true,
      // 添加其他需要更新的属性
    };

    const updatedUser = await graphClient.users.update(userId, updatedUserProperties);

    console.log('用户更新成功:', updatedUser);
    
    return {
      statusCode: 200,
      body: JSON.stringify({ message: '用户更新成功' }),
    };
  } catch (error) {
    console.error('错误:', error);

    return {
      statusCode: 500,
      body: JSON.stringify({ message: '内部服务器错误' }),
    };
  }
};

2. 检查依赖兼容性

  • 确保@azure/identity和@azure/graph版本兼容,建议安装最新稳定版:
    npm install @azure/identity@latest @azure/graph@latest
    
  • 运行npm list @azure/identity @azure/graph查看当前版本,避免版本不匹配导致的导出结构变化。

3. 确认Lambda运行时版本

  • 确保Lambda使用的Node.js版本≥14.x(支持ESM模块和顶层await),在Lambda控制台的函数配置中检查并更新运行时。

4. 验证部署包

  • 确认部署到Lambda的ZIP包包含完整的node_modules目录,没有遗漏@azure/identity和@azure/graph依赖。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 20:18:18