【问题标题】:crypto.randomBytes is not a function in angularcrypto.randomBytes 不是角度函数
【发布时间】:2017-12-11 22:22:04
【问题描述】:

我在angular4中导入ethereumjs-wallet

import EthereumWallet from 'ethereumjs-wallet';
var wallet = EthereumWallet.generate();
const jsV3 = wallet.toV3(passwd);

库内代码如下所示

var privKey = crypto.randomBytes(32)

抛出一个错误

ERROR Error: Uncaught (in promise): TypeError: crypto.randomBytes is 不是函数类型错误:crypto.randomBytes 不是函数 TypeError:crypto.randomBytes 不是函数 在 Function.webpackJsonp.../../../../ethereumjs-wallet/index.js.Wallet.generate

如何解决?

【问题讨论】:

  • 您拥有的代表数量,人们会认为您会知道如何提出问题。一行代码对诊断问题没有真正帮助:)
  • 我已经更新了问题

标签: angular


【解决方案1】:

运行

npm install crypto-browserify --save

之后,您需要创建一个文件来修复(解决方法)...像这样:

var fs = 需要('fs'); // PACKAGE_ERROR 和 FILE_ERROR 显示在您的错误中 fs.readFile('./node_modules/[PACKAGE_ERROR]/[FILE_ERROR]', 'utf8', function (err,data) { 如果(错误)返回控制台.log(错误); var 结果 = data.replace('require(\'crypto\');', 'require(\'crypto-browserify\');'); fs.writeFile('./node_modules/[PACKAGE_ERROR]/[FILE_ERROR]', 结果, 'utf8', function (err) { 如果(错误)返回控制台.log(错误); }); });

现在,您需要在 package.json 中添加安装后代码

... “脚本”:{ "postinstall":"节点 [fixFileName].js", ...

最后,运行:

npm install
ng serveng build

我希望这会有所帮助!

【讨论】:

  • 当我尝试这个时,我得到: { [Error: ENOENT: no such file or directory, open './node_modules/[PACKAGE_ERROR]/[FILE_ERROR]'] errno: -2, code: 'ENOENT',系统调用:'open',路径:'./node_modules/[PACKAGE_ERROR]/[FILE_ERROR]' }
猜你喜欢
  • 1970-01-01
  • 2020-05-09
  • 2021-05-02
  • 2020-06-20
  • 1970-01-01
  • 2018-04-19
  • 2016-11-07
  • 1970-01-01
  • 2020-07-18
相关资源
最近更新 更多