无法在React Typescript中解析'crypto-js'

问题描述 投票:0回答:2

我试图在React Typescript中使用crypto-js,但出现以下错误

Module not found: Can't resolve 'crypto-js' in '/app/crypto'

这里是我导入crypto-js。例如:

import crypto from 'crypto-js';

export const encrypt = (key: string, evalue: any) => {
    const secret_key = crypto.SHA256(key);
    return crypto.AES.encrypt(evalue, secret_key).toString();
};

请帮我解决这个问题,我已经尝试了很多次了,但是没有用。

reactjs typescript encryption cryptojs
2个回答
0
投票

我最好的猜测是Typescript需要软件包的一些类型定义。

尝试运行npm install @types/crypto-js --save-dev


0
投票

关于tsconfig.json文件,我相信这是因为您指定了"include": [ "src" ],所以这意味着将跳过所有node_modules。尝试将其删除并改用baseUrl选项。

构建项目不需要类型(除非您使用strict标志)。但是正如@Kael所说,安装它们可能很有用。所以你可以指定

"typeRoots": [
  "node_modules/@types"
],

在您的配置文件中

© www.soinside.com 2019 - 2024. All rights reserved.