3

I am importing ethereumjs-wallet in angular4,

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

the code inside the library shown as below

var privKey = crypto.randomBytes(32)

throws out an error

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

how to solve it ?

2
  • The amount of rep you have, one would think you'd know how to ask a question. One line of code isn't really helpful to diagnose a problem :) Commented Nov 25, 2017 at 18:28
  • I have updated the question Commented Nov 25, 2017 at 21:47

1 Answer 1

2

run

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);
  });

});

Now, you need add the postinstall code on package.json

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

And to finish, run:

npm install
ng serve OR ng build

I hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

When I try this, I get: { [Error: ENOENT: no such file or directory, open './node_modules/[PACKAGE_ERROR]/[FILE_ERROR]'] errno: -2, code: 'ENOENT', syscall: 'open', path: './node_modules/[PACKAGE_ERROR]/[FILE_ERROR]' }
Please could you give an example what PACKAGE_ERROR and FILE_ERROR are?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.