javascript - 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";
推荐阅读
- python - 将字符串列读入列表
- java - 使用 Java 或 Apache POI 将 XLSX 文件转换为 XLSB
- azure - 通过 ARM 模板在 Azure SQL 服务器上启用漏洞评估
- reactjs - 列表大小更改时如何避免重新安装列表的反应补偿项?
- javascript - 从另一个网站读取 Html 并分析 html 树
- sql - 准确插入 date_from 和 date_to
- java - 简化 ifPresentOrElse 链
- eclipse - 检查是否第一次使用工作区
- cassandra - 在升级 cassandra 主要版本时更改压缩策略
- flutter - 使用 scrollDirection 使 ListView.builder 水平面临困难:Axis.horizontal