I'm trying to auto update my front-end repo with the contract address, but I keep getting TypeError: Cannot read properties 'toString'), I tried removing the toString and it prompted me to convert toString, I imported network from hardhat so im not sure what's wrong.enter image description here

const { ethers, network } = require("hardhat");
const fs = require("fs");
async function updateContractAddress() {
    const chainId = network.config.chainId.toString();
    const nftMarketPlace = await ethers.getContract("BullChordMarket");
    const contractAddresses = JSON.parse(
        fs.readFileSync(frontEndContractsFile, "utf8")
    if (chainId in contractAddresses) {
        if (
        ) {
    } else {
        contractAddresses[chainId] = {
            BullChordMarket: [nftMarketPlace.address],

    fs.writeFileSync(frontEndContractsFile, JSON.stringify(contractAddresses));
  • Try to log the chainId in the console
    – Majd TL
    Commented Aug 9, 2022 at 19:29
  • It breaks before it gets to the log, so I get no output
    – Santos
    Commented Aug 9, 2022 at 19:43
  • @Santos First, are you sure the error is caused in that section of the code? In that case add more logs before the error. Try logging network.config or even network.
    – Ismael
    Commented Aug 10, 2022 at 4:32

1 Answer 1


The error log means that chainId is undefined.

When you run the deploy script, you can specify which network config you want to use, it usually defines inside hardhat.config.js (see documentation). You can console log the network object first to see if it's correctly setup and contains correct config object and config.chainId.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.