JavaScript错误:找不到导入的模块

弗雷德

我试图从给定目录导入模块“ CryptographyClient”,它在打字稿中成功。但是,将代码编译为javascript后,出现错误消息,提示找不到模块。以下是我的打字稿代码:

import { CryptographyClient } from "C:/Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/src/cryptographyClient";
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");

  const cryptoClient = new CryptographyClient(url, myWorkKey.keyMaterial!.kid!, 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);
});

我希望代码可以平稳运行,但是在终端中出现了一个错误:

Error: Cannot find module 'C:/Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/src/cryptographyClient'
明晰

您需要使用相对导入而不是绝对导入。只需从包中导入:

import { CryptographyClient } from "@azure/keyvault-keys";

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档