【问题标题】:Can't resolve 'crypto' in './node_modules/sjcl' while using SJCL in Angular 7在 Angular 7 中使用 SJCL 时无法解析 './node_modules/sjcl' 中的 'crypto'
【发布时间】:2019-03-29 07:30:26
【问题描述】:

我想在客户端加密数据,所以我找到了 SJCL npm。

浏览他们的文档。但是,SJCL 贡献者提供的文档较少,无法在 Angular 中实现

我已经尝试了以下方法。

我安装了包

  1. npm install --save sjcl

    之后我也尝试安装类型定义

  2. npm install --save sjcl @types/sjcl

这是我的 package.json

{

"name": "project-name",

"version": "0.0.0",


  "scripts": {

    "ng": "ng",

    "start": "ng serve",

    "build": "ng build",

    "test": "ng test",

    "lint": "ng lint",

    "e2e": "ng e2e"

  },

  "private": true,

  "dependencies": {

    "@angular/animations": "~7.1.0",

    "@angular/cdk": "~7.1.0",

    "@angular/common": "~7.1.0",

    "@angular/compiler": "~7.1.0",

    "@angular/core": "~7.1.0",

    "@angular/forms": "~7.1.0",

    "@angular/platform-browser": "~7.1.0",

    "@angular/platform-browser-dynamic": "~7.1.0",

    "@angular/router": "~7.1.0",

    "@types/axios": "^0.14.0",

    "@types/sjcl": "^1.0.28",

    "angular-filepond": "^1.0.5",

    "core-js": "^2.5.4",

    "rxjs": "~6.3.3",

    "sjcl": "^1.0.8",

    "tslib": "^1.9.0",

    "zone.js": "~0.8.26"

  },

  "devDependencies": {

    "@angular-devkit/build-angular": "~0.11.0",

    "@angular/cli": "~7.1.2",

    "@angular/compiler-cli": "~7.1.0",

    "@angular/language-service": "~7.1.0",

    "@schematics/angular": "~7.1.0",

    "@types/echarts": "^4.1.3",

    "@types/jasmine": "~2.8.8",

    "@types/jasminewd2": "~2.0.3",

    "@types/node": "^8.9.5",

    "codelyzer": "~4.5.0",

    "jasmine-core": "~2.99.1",

    "jasmine-spec-reporter": "~4.2.1",

    "karma": "~3.1.1",

    "karma-chrome-launcher": "~2.2.0",

    "karma-coverage-istanbul-reporter": "~2.0.1",

    "karma-jasmine": "~1.1.2",

    "karma-jasmine-html-reporter": "^0.2.2",

    "protractor": "~5.4.0",

    "ts-node": "~7.0.0",

    "tslint": "~5.11.0",

    "typescript": "~3.1.6"

  }
}

我也这样做了

import * as sjcl from 'sjcl';

在我的一个组件中。

此外,我已通过此链接 Using SJCL library in Angular2

当我运行 Angular 应用程序时,我得到了

** WARNING in ./node_modules/sjcl/sjcl.js
Module not found: Error: Can't resolve 'crypto' in './node_modules/sjcl'**


**Note starts:**

used sjcl.encrypt("password", "data")
but after console.log(sjcl.encrypt("password", "data"))
it shows undefined

**Note ends:**

可能是我的注释不是那么完美,或者我定义错误。如何使用 SJCL 加密数据而不出现此警告?

【问题讨论】:

    标签: angular cryptography sjcl


    【解决方案1】:

    我遇到了同样的问题,经过一番研究,我发现这是与节点相关的警告。使用 Angular 你需要修改文件:

    node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js

    在我更改的文件末尾

    节点:假,

    进入

    节点:{crypto:true,stream:true},

    【讨论】:

    • Bravo 在这里看到一些答案......但是,我还没有实现你的代码
    【解决方案2】:

    我已将 sjcl 库用于我的 react 应用程序。 但过程保持不变。

    import sjcl from 'sjcl';
    

    现在您可以使用 sjcl.encrypt() 函数。 如果您正在寻找 AES 加密的一些示例,您可以查看我的详细回答 https://stackoverflow.com/a/60504619/6453913

    你也可以使用传统的 commonJs require 方法。

    【讨论】:

    • 我不是在寻找反应导入,因为我有角码。此外,Angular 在版本控制方面进展得如此之快,并在升级的版本控制中引入了 IVY...将在即将到来的项目中使用 Angular... katar 支持 Angular :-)
    • React/Angular 或任何东西取决于用例。此外,我刚刚测试了它的角度,它按预期工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    • 2013-04-08
    • 2015-12-16
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多