formatBytes32String stopped working after moving to ethers v6
Your error
TypeError: Cannot read properties of undefined (reading 'formatBytes32String')3 more lines it can showFix it fast
Most common cause, 1 of 3v5 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.
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.0Short 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.
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.
npm ls ethersThen find the line you see in this list and open its fix:
- 1v5 code runs on ethers v6, where utils is gone and the helper was renamed
npm ls ethers prints 6.x and the error reads (reading 'formatBytes32String') or ethers.formatBytes32String is not a function - 2The text is longer than 31 bytes once encoded as UTF-8
The renamed call runs but throws bytes32 string must be less than 32 bytes for some inputs only - 3decodeBytes32String gets a value that was never a bytes32 string
Decoding 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 functionError: bytes32 string must be less than 32 bytesError: 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.
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.
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.0Verify
The script prints a 0x value of 64 hex digits for the encoded text, and decoding it returns hello.
node -e "const { ethers } = require('ethers'); const b = ethers.encodeBytes32String('hello'); console.log(b, ethers.decodeBytes32String(b))"Confirmed by docs.ethers.org
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.
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.
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.
node encode-labels.mjsConfirmed by raw.githubusercontent.com
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.
node -e "const { ethers } = require('ethers'); const b = ethers.getBytes(process.argv[1]); console.log(b.length, b[31])" 0xYOUR_VALUEFix
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.
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.
node decode-value.mjsConfirmed by raw.githubusercontent.com
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.