2017-11-25 188 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

我希望這有助於!