'Unable to run ethers.getSigner() from the (nodeJS?) console

I am following a Hardhat intro tutorial by Reanblock https://www.youtube.com/watch?v=osHk0eEsjDM and I am stuck in the last few minutes of the video.

I have a simple hardhat.config.js file as follows:

require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-web3"); 
require('solidity-coverage');

task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
  const accounts = await hre.ethers.getSigners();

  for (const account of accounts) {
    console.log(account.address);
  }
});

task("azbalance", "-prints a/c balances- ")
  .addParam("azaccount", "the accounts address")
  .setAction(async (taskArgs) => {
    const account = web3.utils.toChecksumAddress(taskArgs.azaccount);
    const balance = await web3.eth.getBalance(account);
    console.log(web3.utils.fromWei(balance, "ether"), "ETH");
  })


/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {
  solidity: "0.8.4",
};

I successfully created a node in another terminal using the command:

npx hardhat node

I am able to deploy the contract using the following command.

npx hardhat run scripts/sample-script.js --network localhost

on the console I type:

npx hardhat console --network localhost

so far it has been working as expected.

on the (NodeJS ? ) console when I type

acc = await ethers.getSigner()

I get the following error:

Uncaught TypeError: ethers.getSigner is not a function
    at REPL1:1:47

my goal is to get the account address using the command:

acc.address

**** Apologies if used the terms Terminal and Console incorrectly. I'm yet to figure out its proper usage ***



Solution 1:[1]

Where do you run the command npx hardhat console --network localhost?

  • Make sure you run from the folder contained the hardhat.config.js file.
  • Please try with the simple config first:
require("@nomiclabs/hardhat-waffle");

module.exports = {
  solidity: "0.8.4",
  defaultNetwork: "localhost",
  networks: {
    localhost: {
      url: "http://127.0.0.1:8545"
    }
  }
};
  • You should able to achieve

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PySoL