Type is not a constructor during hardhat-gas-reporter initialization
0 reputation · 17 Jul 2025, 00:17 UTC
0 reputation · 17 Jul 2025, 00:17 UTC
The issue seems linked to a version mismatch between the ethers.js dependency defined in the project and the internal versions expected by the Hardhat environment. While the package.json may show compatible major versions, the dependency tree fails to align the provider classes correctly during the gas reporter's execution phase.
What version ranges of ethers and @nomicfoundation/toolbox are guaranteed to be compatible to avoid this constructor error? Does the internal E.js version used by Hardhat contribute to this resolution conflict when external ethers providers are present?
35775 reputation · 17 Jul 2025, 11:32 UTC
The error is caused by having ethers declared in your package.json while @nomicfoundation/toolbox already bundles its own ethers version. The gas reporter loads ethers from the project's node_modules, but Hardhat's runtime injects the toolbox-bundled copy—two different constructor functions collide during initialization.
If you need ethers in your own scripts, import it from Hardhat instead:
const { ethers } = require('hardhat');
// or
import { ethers } from 'hardhat';
Yes, Hardhat's internal ethers version contributes to the conflict. The runtime makes its bundled ethers available globally for plugins. When your project also declares ethers, the resolver may hoist a different major version (v5 vs v6) or even a different minor build. The gas reporter's GasReporter class and Hardhat's EthersProviderWrapper then receive different constructor references, breaking instanceof checks and causing "Type is not a constructor".
npm ls ethers (or pnpm why ethers). You should see exactly one version, matching the toolbox-bundled version.cat node_modules/@nomicfoundation/toolbox/package.json | grep -A2 '"ethers"'.ethers from your package.json (both dependencies and devDependencies), delete node_modules and lockfile, then reinstall.npx hardhat test — the gas reporter should initialize without the constructor error.What does npm ls @nomicfoundation/toolbox ethers show in your project? If the output lists more than one ethers version, the fix above applies. If it already shows a single version matching toolbox's bundle but the error persists, share the exact toolbox version and the full error stack trace—this would indicate a different root cause (e.g., TypeScript @types/ethers mismatch or a plugin like hardhat-deploy pulling its own ethers).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.