【问题标题】:Javascript error: cannot find module importedJavascript 错误:找不到导入的模块
【发布时间】:2019-08-09 08:20:59
【问题描述】:

我试图从给定目录导入模块“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'

【问题讨论】:

  • 尝试在cryptographyClient 上包含.js 文件扩展名
  • 尝试使用相对导入而不是绝对导入。
  • 使用绝对导入非常危险
  • @phuzi 'cryptographyClient' 文件是一个 '.ts' 文件,我尝试添加 '.ts' 扩展名,但被提醒我不需要。
  • 为什么不按照文档中的说明导入呢? import { CryptographyClient } from "@azure/keyvault-keys";

标签: javascript typescript


【解决方案1】:

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

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

【讨论】:

  • 实际上,我运行了npm install @azure/keyvault-keys@4.0.0-preview.2 命令,然后当我尝试import { CryptographyClient } from "@azure/keyvault-keys"; 时收到一条错误消息,指出Module '"../../../../../../../../Users/fredg/Desktop/AzureSDK-master/AzureSDK-master/Node/sample/node_modules/@azure/keyvault-keys/types/keyvault-keys"' has no exported member 'CryptographyClient'. 所以我在github 上搜索了缺少的模块并尝试将它们添加到我自己正确的目录。现在我意识到这是一个非常糟糕的主意。
  • 我应该等待他们修改包而不是试图找出缺少的东西,我仍在学习这种编程语言,非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 2017-05-19
  • 2015-03-23
  • 1970-01-01
  • 2019-12-06
  • 2019-05-25
相关资源
最近更新 更多