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

TypeScript编译为JS后无法找到CryptographyClient模块求助

解决编译JS后找不到CryptographyClient模块的问题

你遇到的问题根源在于直接引用了node_modules里src目录下的源码文件——TypeScript在开发时能找到本地的源码,但编译成JavaScript后,运行时环境找不到这个路径,因为@azure/keyvault-keys包发布到npm的是编译后的产物,并不会包含src目录里的文件。

具体修复步骤:

  • 修正导入路径:不要直接导入src下的文件,而是使用包的公开导出路径。
  • 补充缺失的KeysClient导入:你的代码里用到了KeysClient但没有导入,这也是潜在的运行错误。
  • 确保依赖正确安装:确认已经安装了@azure/keyvault-keys和@azure/identity这两个包。

修正后的完整代码:

// 用正确的包导入方式,替换本地路径
import { CryptographyClient, KeysClient } from "@azure/keyvault-keys";
import { DefaultAzureCredential } from "@azure/identity";
import * as crypto from 'crypto';

async function main(): Promise<void> {
  // DefaultAzureCredential expects the following three environment variables:
  // - AZURE_TENANT_ID: The tenant ID in Azure Active Directory
  // - AZURE_CLIENT_ID: The application (client) ID registered in the AAD tenant
  // - AZURE_CLIENT_SECRET: The client secret for the registered application
  const credential = new DefaultAzureCredential();
  const vaultName = process.env["KEYVAULT_NAME"] || "keyvault-js"
  const url = `https://${vaultName}.vault.azure.net`;
  
  // Connection to Azure Key Vault
  const client = new KeysClient(url, credential);
  let keyName = "localWorkKey";
  
  // Connection to Azure Key Vault Cryptography functionality
  let myWorkKey = await client.createKey(keyName, "RSA");
  // 注意:CryptographyClient的构造参数可以直接传入key对象,不需要手动拼接url和kid
  const cryptoClient = new CryptographyClient(myWorkKey, credential);
  
  // Sign and Verify
  const signatureValue = "MySignature";
  let hash = crypto.createHash("sha256");
  hash.update(signatureValue);
  let digest = hash.digest();
  console.log("digest: ", digest);
  
  const signature = await cryptoClient.sign("RS256", digest);
  console.log("sign result: ", signature);
  
  const verifyResult = await cryptoClient.verify("RS256", digest, signature.result);
  console.log("verify result: ", verifyResult);
  
  // Encrypt and decrypt
  const encrypt = await cryptoClient.encrypt("RSA1_5", Buffer.from("My Message"));
  console.log("encrypt result: ", encrypt);
  
  const decrypt = await cryptoClient.decrypt("RSA1_5", encrypt.result);
  console.log("decrypt: ", decrypt.result.toString());
  
  // Wrap and unwrap
  const wrapped = await cryptoClient.wrapKey("RSA-OAEP", Buffer.from("My Message"));
  console.log("wrap result: ", wrapped);
  
  const unwrapped = await cryptoClient.unwrapKey("RSA-OAEP", wrapped.result);
  console.log("unwrap result: ", unwrapped);
  
  await client.deleteKey(keyName);
}

main().catch((err) => {
  console.log("error code: ", err.code);
  console.log("error message: ", err.message);
  console.log("error stack: ", err.stack);
});

额外说明:

  1. 我还优化了CryptographyClient的实例化方式——直接传入myWorkKey对象即可,不需要手动处理url和kid,这是官方推荐的用法。
  2. 运行前记得设置好AZURE_TENANT_ID、AZURE_CLIENT_ID、AZURE_CLIENT_SECRET和KEYVAULT_NAME这几个环境变量。
  3. 如果之前安装依赖有问题,可以重新执行:
npm install @azure/keyvault-keys @azure/identity

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:46:27