NativeScriptException:无法找到模块:“ crypto”

时间:2019-04-01 15:22:48

标签: angular cryptography nativescript

我试图在nativescript中运行openpgp库以使用其加密,在尝试使用它安装并运行它之后,出现此错误

JS: Angular is running in the development mode. Call enableProdMode() to enable the production mode.
JS: ERROR Error: Uncaught (in promise): Error: com.tns.NativeScriptException: Failed to find module: "crypto", relative to: app/tns_modules/

我的package.json就是这样

  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/http": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@mattiasbuelens/web-streams-polyfill": "^0.3.2",
    "address-rfc2822": "^2.0.4",
    "asmcrypto.js": "^2.3.2",
    "asn1.js": "^5.0.1",
    "bn.js": "^4.11.8",
    "browserify-derequire": "^1.0.0",
    "buffer": "^5.2.1",
    "compressjs": "^1.0.3",
    "crypto-js": "^3.1.9-1",
    "elliptic": "^6.4.1",
    "hash.js": "^1.1.7",
    "nativescript-angular": "^7.2.3",
    "nativescript-localstorage": "^2.0.0",
    "nativescript-nodeify": "^0.8.0",
    "nativescript-theme-core": "~1.0.4",
    "node-fetch": "^2.3.0",
    "node-localstorage": "^1.3.1",
    "openpgp": "^4.4.10",
    "reflect-metadata": "~0.1.12",
    "rusha": "^0.8.13",
    "rxjs": "~6.3.0",
    "rxjs-compat": "^6.4.0",
    "tns-core-modules": "^5.4.0-next-2019-03-29-153458-02",
    "web-stream-tools": "0.0.1",
    "zone.js": "~0.8.26"
   }

如果有人知道某事,请您告诉我, 如果您知道如何在本机脚本中使用开放式pgp库,那将非常有帮助

2 个答案:

答案 0 :(得分:0)

如果您的NPM程序包依赖于Node引擎中的程序包,例如crypto,则它将不能与NativeScript一起使用。

一种解决方法是使用nativescript-nodeify插件,该插件内置支持Node引擎中的少数软件包,例如fscrypto等,

答案 1 :(得分:0)

您可以使用我的openpgp.js v2.5.9的分支,其中包括一些polyfill,以使openpgp.js在Nativescript上工作​​。您只需要执行npm install github:mohammadrafigh/nativescript-openpgpjs#openpgp2并像这样导入openpgp:

import * as openpgp from "openpgp/dist/openpgp";

我将使用来自openpgp 4的最新更改来更新此存储库,并尽快将其作为单独的软件包发布在npm上,但现在确保您使用的是openpgp2分支而不是master。