1

I was trying to import a module 'CryptographyClient' from a given directory, it was successful in typescript. However, after I compiled the code into javascript, I got an error saying it cannot find module. Below is my 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 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);
});

I expected the code to run smoothly, however in the terminal I got an error:

Error: Cannot find module 'C:/Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/src/cryptographyClient'
6
  • 1
    Try including the .js file extension on cryptographyClient Commented Aug 9, 2019 at 8:22
  • 1
    Try using relative imports instead of absolute ones. Commented Aug 9, 2019 at 8:23
  • using the absolute imports is very dangerous Commented Aug 9, 2019 at 8:26
  • @phuzi the 'cryptographyClient' file was a '.ts' file, I tried to add '.ts' extension and I got reminded that I don't need to. Commented Aug 9, 2019 at 8:28
  • Why don't you import it as specified in the documentation? import { CryptographyClient } from "@azure/keyvault-keys"; Commented Aug 9, 2019 at 8:31

1 Answer 1

2

You need to use relative imports instead of absolute ones. Just import from the package:

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

Sign up to request clarification or add additional context in comments.

2 Comments

Acutually , I ran the npm install @azure/[email protected] command and then when I tried import { CryptographyClient } from "@azure/keyvault-keys"; I got an error message saying that Module '"../../../../../../../../Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/types/keyvault-keys"' has no exported member 'CryptographyClient'. So I searched for the missing modules on github and tried to add them in the correct directories myself. Now I realised that it is a very bad idea.
I should have waited for them to modify the package instead of trying to find out what is missing, I am still learning on this programming language, thanks a lot for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.