2017-11-25 192 views
1

我导入在angular4 ethereumjs-wallet的函数,crypto.randomBytes不在角

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 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

库中的代码

如何解决?

+0

代表你有多少代表,你会知道如何提出问题。一行代码对诊断问题并不是很有用:) – Alex

+0

我已经更新了这个问题 – user824624

回答

2

运行

npm install crypto-browserify --save

后,你需要创建一个文件来解决(解决方法)......这样的:

 
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安装后的代码

 
... 
"scripts":{ 
"postinstall":"node [fixFileName].js", 
... 

而完成,运行:

npm install
ng serve OR ng build

我希望这有助于!