我试图从给定目录导入模块“ 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'
答案 0 :(得分:1)
您需要使用相对进口而不是绝对进口。只需从软件包中导入:
import { CryptographyClient } from "@azure/keyvault-keys";