crypto.randomBytes不是角度函数 [英] crypto.randomBytes is not a function in angular
问题描述
我要在angular4中导入 ethereumjs-wallet
I am importing ethereumjs-wallet in angular4,
import EthereumWallet from 'ethereumjs-wallet';
var wallet = EthereumWallet.generate();
const jsV3 = wallet.toV3(passwd);
库中的代码如下所示
var privKey = crypto.randomBytes(32)
抛出错误
ERROR错误:未捕获(承诺):TypeError:crypto.randomBytes为 不是函数TypeError:crypto.randomBytes不是函数 TypeError:crypto.randomBytes不是一个函数 在Function.webpackJsonp .../../../../ethereumjs-wallet/index.js.Wallet.generate
ERROR Error: Uncaught (in promise): TypeError: crypto.randomBytes is not a function TypeError: crypto.randomBytes is not a function TypeError: crypto.randomBytes is not a function at Function.webpackJsonp.../../../../ethereumjs-wallet/index.js.Wallet.generate
如何解决呢?
推荐答案
运行
npm install crypto-browserify --save
然后,您需要创建一个文件来修复(解决方法)...就像这样:
and after, you need create a file to fix (workaround)... like this:
var fs = require('fs');
// PACKAGE_ERROR and FILE_ERROR is shown on your error
fs.readFile('./node_modules/[PACKAGE_ERROR]/[FILE_ERROR]', 'utf8', function (err,data) {
if (err) return console.log(err);
var result = data.replace('require(\'crypto\');', 'require(\'crypto-browserify\');');
fs.writeFile('./node_modules/[PACKAGE_ERROR]/[FILE_ERROR]', result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
现在,您需要在package.json上添加安装后代码
Now, you need add the postinstall code on package.json
...
"scripts":{
"postinstall":"node [fixFileName].js",
...
最后,运行:
npm install
ng服务或 ng构建
npm install
ng serve OR ng build
我希望这会有所帮助!
这篇关于crypto.randomBytes不是角度函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!