Web3: max code size exceeded when deploying a contract
EIP-170 caps deployed runtime code at 24576 bytes and the deployment is rejected once the compiled contract passes it. The growth is usually incidental rather than a single large addition: long revert strings, many public functions and inlined library code all count, and building with the optimiser off can be the whole difference.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
# Measure first, the number tells you how much has to go
forge build --sizes
npx hardhat compile && npx hardhat size-contracts
// The optimiser alone often brings it back under the cap
// hardhat.config.js
solidity: { version: '0.8.28', settings: { optimizer: { enabled: true, runs: 200 } } }
// foundry.toml: optimizer = true, optimizer_runs = 200
// Custom errors replace revert strings and cost bytecode as well as gas
error NotOwner();
// External library functions deploy separately and are linked in
library Fees { function apply(uint a) external pure returns (uint) { return a; } }
// Past that, split the contract: a proxy with facets keeps each piece small
# Local chains can lift the limit for tests, mainnet cannot
# anvil --code-size-limit 50000
How to diagnose Web3 errors
Web3 errors are unusually terse because the EVM reports failure without explanation by default. execution reverted means a require or revert triggered. The reason string is present in the return data but most libraries discard it. Nonce errors reflect the strict sequential ordering of transactions per account, so a stuck transaction blocks every one behind it.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Simulate before sending:
eth_callagainst the same block returns the revert reason string that a submitted transaction hides. - Use a transaction tracer (Tenderly, Foundry's
cast run, or a node'sdebug_traceTransaction) to see the exact opcode and contract where execution reverted. - For nonce errors, query the account's pending nonce (
eth_getTransactionCountwith thependingtag) rather than tracking it locally. - To replace a stuck transaction, resend with the same nonce and a materially higher gas price: most nodes require at least a 10% increase.
- Check the chain ID matches the network you intend. Signing for the wrong chain produces confusing rejection messages.
Tools worth reaching for
cast (Foundry)eth_call simulationTenderly / block explorer tracerdebug_traceTransaction
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related Web3 errors
- ethers v6: Cannot read properties of undefined (reading 'Web3Provider')ethers v6 removed the providers, utils and constants namespaces, so v5 code such as…
- MetaMask Tx Signature: User denied transaction signature (4001)Code 4001 is the EIP-1193 "user rejected" error: the person clicked Reject or closed the…
- Solidity: CompilerError: Stack too deep. Try compiling with --via-irThe EVM can only reach the top 16 slots of its stack, and the function the error points at…
- Web3: already known / ALREADY_EXISTS when sending a transactionThe node already holds a transaction with this exact hash in its pool, so the resend is…
- Web3: cannot estimate gas; transaction may fail or may require manual gas limitThe node ran the transaction against current state and it reverted, so no gas figure could be…
- Web3: could not detect network (event="noNetwork", code=NETWORK_ERROR)ethers asks the endpoint for its chain id before it will do anything else, and that call…
- Web3: execution revertedThe smart contract reverted. Solidity custom errors or revert strings often explain the exact…
- Web3: gas required exceeds allowance (0)The node simulated the transaction and found the sender cannot cover it, so the allowance it…
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.