'Cannot read properties of undefined (reading 'toHexString') in ether.js

Please don't judge, i've no idea what to do and how to do :)

My code: ether.js ( ^5.6.0)

import { ThirdwebSDK } from '@3rdweb/sdk'
import { ethers } from 'ethers'

const sdk = new ThirdwebSDK(
  new ethers.Wallet(
    process.env.METAMASK_PRIVATE_KEY,
    ethers.getDefaultProvider(
      'https://rinkeby.infura.io/v3/'
    )
  ) 
)

bug enter image description here



Solution 1:[1]

You most likely messed up env variable. Check if METAMASK_PRIVATE_KEY is the correct name. Because otherwise it will throw undefined and one of the ethersjs library will try to run hexToString() method on undefined value, thus you get the error.

EDIT:

You might as well forget to include this in your code:

import {} from 'dotenv/config' 

// or if its not ESmodule 

require('dotenv').config()

Absence of this import will throw undefined as well when you access env variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kaneda