Ethereum Hardhat Error: TypeError cannot read properties of Undefined
As a web3 developer, it is not uncommon to encounter errors when working with the Ethereum blockchain using tools like Hardhat. In this article, we will address a common issue related to hardhat deployment on Node.js that is often encountered by new users.
Error message: TypeError: Cannot Read Properties of Undefined (reading ‘0’)
When you run yarn hardhat deploy, your project should successfully compile and deploy your smart contracts. However, if you encounter the following error message:
TypeError: Cannot read properties of undefined (reading '0')'
this indicates that there is a problem with the way Hardhat handles your deployment process.
Why this error occurs

TheTypeError: Cannot Read Properties of Undefinederror occurs when Hardhat tries to access the0index of an array or object, but it does not exist. In your case, this may be related to the way you are using thedeployfunction of hardhat.
Solution 1: Check your deployment code
One possible reason for this error is that your deployment code is not setting up your deployment environment correctly. Here are a few things to check:
- Make sure yourhardhat.config.js
file is configured correctly, including thenetworksanddeployersettings.
- Make sure yourdeploy
function is being called with the correct arguments.
Here is an example of what your deployment code might look like:
module.exports = {
// ... other configurations ...
networks: {
mainnet: {
accounts: [
'0x1234567890123456789012345678901234567890',
'0x9876543210987654321098765432109876543210'
],
gas: 200000,
Gas price: 20
}
},
installer: {
network: 'mainnet',
// ... other settings ...
},
async deploy() {
await this.deployed();
return this.state;
}
};
Solution 2: Use the etherslibrary
Another possible cause of this error is that you are trying to access an undefined object. In your case, it may be related to the use of theetherslibrary.
To fix this issue, make sure you have installed and imported theethers.jslibrary correctly:
const ethers = require('ethers');
// ... other code ...
You can also try to initialize ethers globally in your project's package.jsonfile:
"dependencies": {
"@nomiclabs/ethers": "^5.2.0",
// ... other dependencies ...
}
Solution 3: Check your hardhat configuration
Hardhat has a configuration system that allows you to customize the deployment process. Here are a few things to check:
- Make sure your
hardhat.config.jsfile is properly configured, including any custom settings or overrides.
- Make sure your
ethersProvideris properly set up.
To fix this issue, make sure you initialize ethers properly in your project’s main file:
import * as ethers from 'ethers';
const provider = new ethers.providers.Web3Provider(window.ethereum);
const networkId = window.ethereum?.chainId;
// ... other code ...
By checking these possible solutions and trying them out, you should be able to resolve the TypeError: Cannot Read Properties of Undefined error when deploying your smart contracts with hardhat.
