【问题标题】:Cannot find module 'crypto-js' or its corresponding type declarations找不到模块 \'crypto-js\' 或其对应的类型声明
【发布时间】:2022-10-23 16:01:58
【问题描述】:

我有一个项目,这个项目正在显示“二维码”,然后由于某种原因我必须使用某个编码器功能,为此我使用了这个库:

Crypto

我正在尝试使用 Crypto,但出现此错误:

Cannot find module 'crypto-js' or its corresponding type declarations.

文件.tsx:

import CryptoJS from 'crypto-js';

const ENC_KEY =
    '50b3cc356d8f34017b3cce1a021389458b898ae85a816201695d11cb87fa1769';
const IV = '07ed0f192b6d8f36c24bd802e0a52cd4';


/**
 * 
 * @param encryptedQR it should be a hex based string
 * @returns a utf8 based decrypted string
 */
export function decrypt(encryptedQR: string) {
    const key = CryptoJS.enc.Hex.parse(ENC_KEY);
    const iv = CryptoJS.enc.Hex.parse(IV);
    const encryptedHex = CryptoJS.enc.Hex.parse(encryptedQR);
    const encrypted = CryptoJS.enc.Base64.stringify(encryptedHex);
    const decrypted = CryptoJS.AES.decrypt(encrypted, key, {
        iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.NoPadding,
    });
    return CryptoJS.enc.Utf8.stringify(decrypted).trim();
}

【问题讨论】:

    标签: reactjs typescript cryptojs


    【解决方案1】:

    我通过以下步骤解决了我的问题:

    1- npm install crypto-js --save
    2- npm install @types/crypto-js
    3- 
          from: import { CryptoJS } from ‘crypto-js’;
          to: import * as CryptoJS from ‘crypto-js’;
    

    或者

    1- yarn add crypto-js --save
    2- yarn add @types/crypto-js
    3- 
          from: import { CryptoJS } from ‘crypto-js’;
          to: import * as CryptoJS from ‘crypto-js’;
    

    【讨论】:

      猜你喜欢
      • 2021-03-17
      • 2021-08-27
      • 2021-01-19
      • 2022-10-17
      • 2022-11-05
      • 2022-06-25
      • 2022-11-10
      • 2021-08-16
      • 2022-10-22
      相关资源
      最近更新 更多