1

What is wrong here?

const userWalletKeys = Wallet.createRandom().mnemonic
const userWallet = ethers.Wallet.fromMnemonic(userWalletKeys.phrase)

I get this error at line 2 of code: Uncaught TypeError: ethers__WEBPACK_IMPORTED_MODULE_3__.Wallet.fromMnemonic is not a function

I tried to generate a random mnemonic phrase for a ether wallet.

3 Answers3

1

The syntax changed in v6. Instead of

const userWallet = ethers.Wallet.fromMnemonic(userWalletKeys.phrase)

you'll do

 const userWallet = ethers.HDNodeWallet.fromMnemonic(userWalletKeys.phrase)

You can also just import modularly like this:

const { HDNodeWallet } = require('ethers') 

then use just use it without importing the entire ethers library, like this:

const userWallet = HDNodeWallet.fromMnemonic(userWalletKeys.phrase)

see docs here: ethers v6 docs

sung
  • 321
  • 3
  • 7
0

Just been using ChatGPT to debug this very same error and after many twists and turns it suggested to fall back to ethers 5.0.0 (modifying package.json) and that worked. It seems in version 6.1.0 that method has dissappeared or maybe isn't ready yet or something...

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 19 '23 at 08:09
0

In v.6.1.0 you have to use ethers.Wallet.fromPhrase

ruud
  • 743
  • 13
  • 22
vitof
  • 1