Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

formatBytes32String stopped working after moving to ethers v6

Your error

TypeError: Cannot read properties of undefined (reading 'formatBytes32String')3 more lines it can show

Fix it fast

Most common cause, 1 of 3

v5 code runs on ethers v6, where utils is gone and the helper was renamed

Replace both helpers with their v6 names on the root import.

Fix
import { ethers } from "ethers";

const bytes32 = ethers.encodeBytes32String("hello");   // was ethers.utils.formatBytes32String
const text = ethers.decodeBytes32String(bytes32);      // was ethers.utils.parseBytes32String

// named imports work too
import { encodeBytes32String, decodeBytes32String } from "ethers";

// or stay on v5 for now
// npm install ethers@5.8.0
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ethers.utils.formatBytes32String fails in ethers v6 because v6 dropped the utils namespace and renamed the helper. Call ethers.encodeBytes32String(text) to encode and ethers.decodeBytes32String(value) to read it back. Text over 31 UTF-8 bytes still throws, and decoding needs a value made that way. Applies to ethers 6.17, or pin ethers 5.8.0 to keep the old call.

  • Applies to ethers 6.17.0, ethers 5.8.0 (legacy-v5 tag), Node.js 22 and 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

See which ethers major version the project really resolves. A 6.x line with v5-style calls is cause 1; if the call already uses encodeBytes32String, the thrown message points to cause 2 or 3.

Run this first
npm ls ethers

Then find the line you see in this list and open its fix:

  1. 1v5 code runs on ethers v6, where utils is gone and the helper was renamednpm ls ethers prints 6.x and the error reads (reading 'formatBytes32String') or ethers.formatBytes32String is not a function
  2. 2The text is longer than 31 bytes once encoded as UTF-8The renamed call runs but throws bytes32 string must be less than 32 bytes for some inputs only
  3. 3decodeBytes32String gets a value that was never a bytes32 stringDecoding throws invalid bytes32 - not 32 bytes long or invalid bytes32 string - no null terminator

What does the error look like?

  • TypeError: Cannot read properties of undefined (reading 'formatBytes32String')
  • TypeError: ethers.formatBytes32String is not a function
  • Error: bytes32 string must be less than 32 bytes
  • Error: invalid bytes32 string - no null terminator

Cause 1 of 3

v5 code runs on ethers v6, where utils is gone and the helper was renamed

You seenpm ls ethers prints 6.x and the error reads (reading 'formatBytes32String') or ethers.formatBytes32String is not a function

Check

ethers.utils.formatBytes32String breaks on ethers v6 for two reasons that arrive together. The v6 migration guide moves every helper from ethers.utils to the root package, so ethers.utils is undefined, and it renames formatBytes32String to encodeBytes32String and parseBytes32String to decodeBytes32String.

Check
grep -rnE "utils\.(format|parse)Bytes32String|(format|parse)Bytes32String" src/

Fix

Replace both helpers with their v6 names on the root import. If a dependency still needs v5 and you cannot migrate yet, pin the last v5 release instead of mixing styles.

Fix
import { ethers } from "ethers";

const bytes32 = ethers.encodeBytes32String("hello");   // was ethers.utils.formatBytes32String
const text = ethers.decodeBytes32String(bytes32);      // was ethers.utils.parseBytes32String

// named imports work too
import { encodeBytes32String, decodeBytes32String } from "ethers";

// or stay on v5 for now
// npm install ethers@5.8.0

Verify

The script prints a 0x value of 64 hex digits for the encoded text, and decoding it returns hello.

Verify
node -e "const { ethers } = require('ethers'); const b = ethers.encodeBytes32String('hello'); console.log(b, ethers.decodeBytes32String(b))"

Confirmed by docs.ethers.org

Did this fix it?

Cause 2 of 3

The text is longer than 31 bytes once encoded as UTF-8

You seeThe renamed call runs but throws bytes32 string must be less than 32 bytes for some inputs only

Check

encodeBytes32String measures the UTF-8 bytes of the text, not its characters, and needs one spare byte for the zero terminator. Anything over 31 bytes throws, so a 30-letter label with one accented letter or emoji can already be too long.

Check
node -e "const { ethers } = require('ethers'); console.log(ethers.toUtf8Bytes(process.argv[1]).length)" "your label here"

Fix

Check the byte length before encoding and reject or shorten labels over 31 bytes. If longer values are part of the design, the contract field has to change from bytes32 to a type that holds more data.

Fix
import { encodeBytes32String, toUtf8Bytes } from "ethers";

function toBytes32Label(text) {
  if (toUtf8Bytes(text).length > 31) {
    throw new Error(`label too long for bytes32: ${text}`);
  }
  return encodeBytes32String(text);
}

Verify

Labels of 31 bytes or fewer encode, and longer ones fail with your own clear message before any transaction is sent.

Verify
node encode-labels.mjs

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

decodeBytes32String gets a value that was never a bytes32 string

You seeDecoding throws invalid bytes32 - not 32 bytes long or invalid bytes32 string - no null terminator

Check

decodeBytes32String accepts exactly 32 bytes whose last byte is zero, the shape its encoder produces. A hash, an address, a shorter hex value, or text that fills all 32 bytes because a contract wrote it without a terminator fails one of those two checks.

Check
node -e "const { ethers } = require('ethers'); const b = ethers.getBytes(process.argv[1]); console.log(b.length, b[31])" 0xYOUR_VALUE

Fix

Only decode values that hold text. Keep hashes and IDs as hex strings. For a full 32-byte text with no terminator, convert the bytes directly with toUtf8String.

Fix
import { getBytes, toUtf8String } from "ethers";

// 32 bytes of text, no trailing zero byte
const text = toUtf8String(getBytes(valueFromContract));

Verify

The value prints as readable text, and hash-type fields are no longer passed to the decoder.

Verify
node decode-value.mjs

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Pin ethers to one major version in package.json and check npm ls ethers in CI, so a plugin cannot pull a second major in beside it.
  • Wrap bytes32 encoding in one helper that checks the 31-byte limit, instead of calling the library directly across the codebase.
  • When upgrading from v5, search for every ethers.utils, ethers.providers and BigNumber use before the first run; the migration guide lists the replacements.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.