Nodejs 加密模块不适用于打字稿

Posted

技术标签:

【中文标题】Nodejs 加密模块不适用于打字稿【英文标题】:Nodejs crypto module does not work with typescript 【发布时间】:2019-12-28 11:08:49 【问题描述】:

我正在尝试使用 vuejs 和 typescript 在我的项目中实施加密。我在 .vue 文件中成功实现了它,但是当我尝试将加密写入 typescript 类时,mocha 测试会运行它,但是当我尝试在浏览器中编译并打开它时,我在控制台中收到一个 javascript 错误:

app.js:128620 Uncaught ReferenceError: require is not defined
at Object.crypto (app.js:128620)
at __webpack_require__ (app.js:20)
at Object../resources/ts/Classes/Model/Crypter.ts (app.js:125233)
at __webpack_require__ (app.js:20)
at Object../resources/ts/Controller/AuthController.ts (app.js:125924)
at __webpack_require__ (app.js:20)
at Object../node_modules/babel-loader/lib/index.js?!./node_modules/babel- 
loader/lib/index.js!./node_modules/vue- 
loader/lib/index.js?!./resources/js/Components/Includes/Login.vue? 
vue&type=script&lang=js& (app.js:3308)
at __webpack_require__ (app.js:20)
at Module../resources/js/Components/Includes/Login.vue? 
vue&type=script&lang=js& (app.js:124322)
at __webpack_require__ (app.js:20)

app.js中的标记行是:

module.exports = require("crypto");

我已经在打字稿文件中尝试了不同的导入“策略”,例如:

import * as crypto from "crypto";
import crypto from "crypto";
var crypto = require("crypto");

并且还在 webpack.mix.js 中添加这个

"node": 
        fs: "empty",
        crypto: true,
        http: true,
        https: true,
        os: true,
        vm: true,
        stream: true,
        path: true
    ,

但是没有用。

Crypter.ts

import crypto from "crypto";

export default class Crypter 

    public constructor() 
    

    public static publicEncrypt(data: JSON, publicKey: string): string
        let dataStr = JSON.stringify(data);
        let dataB64 = Buffer.from(dataStr).toString("base64");
        var buffer = Buffer.from(dataB64);
        return crypto.publicEncrypt(publicKey, buffer).toString("base64"); 
    

tsconfig.json

    "compilerOptions": 
        "allowUnreachableCode": false,
        "allowUnusedLabels": true,
        "alwaysStrict": true,
        "charset": "utf8",
        "declaration": true,
        "declarationDir": "resources/ts/lib/types",
        "declarationMap": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "lib": ["ES5", "ES6", "DOM", "DOM.Iterable", "ScriptHost"],
        "locale": "en",
        "module": "commonjs",
        "moduleResolution": "node",
        "noFallthroughCasesInSwitch": true,
        "noImplicitAny": true,
        "noImplicitReturns": true,
        "noImplicitThis": true,
        "noImplicitUseStrict": false,
        "noUnusedParameters": true,
        "outDir": "public/js",
        "baseUrl": "resources/",
        "paths": 
            "Base/*":           [ "ts/*" ],
            "@js/*":            [ "js/*" ],
            "@ts/*":            [ "ts/*" ],
            "Component/*":      [ "ts/Components/*" ],
            "Interface/*":      [ "ts/Interfaces/*" ],
            "Model/*":          [ "ts/Model/*" ],
            "Controller/*":     [ "ts/Controller/*" ],
            "Helper/*":         [ "ts/Helper/*" ]
        ,
        "pretty": true,
        "sourceMap": true,
        "target": "ES5",
        "typeRoots": [
            "node_modules/@types"
        ],
        "downlevelIteration": true
    ,
    "include": [
        "resources/ts/**/*"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ],
    "files": [
        "./node_modules/@types/node/index.d.ts"
      ]

我认为错误是由错误的编译或其他错误配置引起的。

【问题讨论】:

【参考方案1】:

您无法在 Web 应用程序中访问 nodejs 加密模块。 Node.js 使用相同的 javascript 语法,但不在浏览器中运行。

您可能需要安装另一个加密库,例如 crypto-js,您可以使用 npm install crypto-js 进行安装。可能还有很多其他库可供您选择 (some ideas)

【讨论】:

以上是关于Nodejs 加密模块不适用于打字稿的主要内容,如果未能解决你的问题,请参考以下文章

NodeJs 打字稿和模块问题

打字稿:具有有效打字的“找不到模块”

打字稿:不能在模块外使用导入语句

打字稿文件中的Nodejs加密

我今天如何在 package.json 中使用“exports”来嵌套子模块和打字稿

打字稿类型检查不适用于数组过滤器