Solidity – Troubleshooting console.log Not Working in Hardhat Scripts

consoleethers.jshardhatjavascriptsolidity

console.log not working in scripts/deploy.js and contracts/WavePortal.sol in Hardhat. When I run the command npx hardhat run scripts/deploy.js it only shows "" Compiled 1 Solidity file successfully "" .
Its not working in my HardHat environment. Not even when I am using INFURA and running with the command npx hardhat run scripts/deploy.js –network ropsten
I searched everywhere from youtube to StackOverflow but didn't got any solution. I only got one hint —>

Since the contract code is running onchain, you'll see the console.log from the chain output, not in your javascript console output. But I am not able to get this…. Please help me

scripts/deploy.js

    const [owner, randomPerson] = await hre.ethers.getSigners();
    const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
    const waveContract = await waveContractFactory.deploy();
    await waveContract.deployed();
    
    console.log("Contract deployed to:", waveContract.address);
    console.log("Contract deployed by:", owner.address);
    
    let wavecount;
    wavecount = await waveContract.getTotalWaves();
    
    let waveTxn = await waveContract.wave();
    await waveTxn.wait();

    wavecount = await waveContract.getTotalWaves();

  };
  
  const runMain = async () => {
    try {
      await main();
      process.exit(0); // exit Node process without error
    } catch (error) {
      console.log(error);
      process.exit(1); // exit Node process while indicating 'Uncaught Fatal Exception' error
    }
    // Read more about Node exit ('process.exit(num)') status codes here: https://stackoverflow.com/a/47163396/7974948
  };

contracts/WavePortal.sol

// SPDX-License-Identifier: UNLICENSED

pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract WavePortal {
    uint256 totalWaves;

    constructor() {
        console.log("Yo yo, I am a contract and I am smart");
    }

    function wave() public {
        totalWaves += 1;
        console.log("%s has waved!", msg.sender);
    }

    function getTotalWaves() public view returns (uint256) {
        console.log("We have %d total waves!", totalWaves);
        return totalWaves;
    }
}

Best Answer

After trying so hard I got the solution. Actually I've installed vs code extension Hardhat+Solidity provided by Hardhat officially. So, I uninstalled it and I installed Solidity extension by Juan Blanco and I downgraded its version to v0.0.135 and now its working absolutely fine !!

Related Topic