angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.73k stars 11.98k forks source link

TypeError: crypto.createHmac is not a function in angular4 #8703

Closed helxsz closed 6 years ago

helxsz commented 6 years ago

Versions

@angular/cli: 1.4.9
node: 7.0.0
os: darwin x64
@angular/animations: 4.4.6
@angular/common: 4.4.6
@angular/compiler: 4.4.6
@angular/core: 4.4.6
@angular/forms: 4.4.6
@angular/http: 4.4.6
@angular/platform-browser: 4.4.6
@angular/platform-browser-dynamic: 4.4.6
@angular/router: 4.4.6
@angular/cli: 1.4.9
@angular/compiler-cli: 4.4.6
@angular/language-service: 4.4.6
typescript: 2.3.4

Observed behavior

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

The same problem applies to

  var mnemonic = bip39.generateMnemonic();
  var privateKey = hdkey.fromMasterSeed(mnemonic)._hdkey._privateKey
  const wallet = EthereumWallet.fromPrivateKey(privateKey)

TradePageComponent.html:1 ERROR TypeError: crypto.createHmac is not a function at Function.webpackJsonp.../../../../hdkey/lib/hdkey.js.HDKey.fromMasterSeed (hdkey.js:162) at Function.webpackJsonp.../../../../ethereumjs- wallet/hdkey.js.EthereumHDKey.fromMasterSeed (hdkey.js:17)

Desired behavior

I guess the problem is due to webpack config not setup properly, how would I config webpack with crypto library?

clydin commented 6 years ago

This is a duplicate of this issue: https://github.com/angular/angular-cli/issues/1548. For details on why this is not configured, please see this comment: https://github.com/angular/angular-cli/issues/1548#issuecomment-286151056.

angular-automatic-lock-bot[bot] commented 5 years ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.