error "fromMnemonic()"Create ether wallet

3k views Asked by At

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.

4

There are 4 answers

1
arynyestos On

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

0
vitof On

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

0
T33 On

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

0
Ayush Saini On

In the version 6.1.0 you need to use const wallet = ethers.Wallet.fromPhrase(mnemonic.phrase) This would return a new HDWallet for you , that would consist of the public and private keys.

const wallet = ethers.Wallet.fromPhrase(mnemonic);
const privateKey = wallet.privateKey;
const publicKey = wallet.publicKey