When attempting to import the 'CryptographyClient' module from a specified directory, I encountered an issue. Initially successful in typescript, but after compiling the code into javascript, an error was thrown stating that it could not find the module. Below is my original typescript code:
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 requires these three environment variables:
// - AZURE_TENANT_ID: Azure Active Directory tenant ID
// - AZURE_CLIENT_ID: Application (client) ID registered with AAD
// - AZURE_CLIENT_SECRET: Client secret for registered application
const credential = new DefaultAzureCredential();
const vaultName = process.env["KEYVAULT_NAME"] || "keyvault-js"
const url = `https://${vaultName}.vault.azure.net`;
// Establishing connection with Azure Key Vault
const client = new KeysClient(url, credential);
let keyName = "localWorkKey";
// Connecting to Azure Key Vault Cryptography features
let myWorkKey = await client.createKey(keyName, "RSA");
const cryptoClient = new CryptographyClient(url, myWorkKey.keyMaterial!.kid!, credential);
// Sign and Verify operations
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 data
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 keys
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);
});
Although I anticipated smooth execution of the code, an error message appeared in the terminal:
Error: Cannot find module 'C:/Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/src/cryptographyClient'