Hardhat – Error ‘namedAccounts’ Does Not Exist in Type ‘HardhatUserConfig’

hardhathardhat-deploytypescript

Unable to add 'namedAccounts' to 'HardhatUserConfig' in hardhat.config.ts as it throws below compilation error.

'namedAccounts' does not exist in type 'HardhatUserConfig'

Below is my config file :

import "@nomiclabs/hardhat-etherscan";
import "hardhat-gas-reporter";
import * as dotenv from 'dotenv';
import { HardhatUserConfig } from "hardhat/config"
dotenv.config();

const RINKEBY_RPC_URL = process.env.RINKEBY_RPC_URL || "";
const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
const ETHERSCAN_APIKEY = process.env.ETHERSCAN_APIKEY;
const GOERLI_RPC_URL = process.env.GOERLI_RPC_URL || "";

const config: HardhatUserConfig = {
  solidity: "0.8.7",
  defaultNetwork: "hardhat",
  networks: {
    rinkeby: {      
      url: RINKEBY_RPC_URL,
      accounts: [PRIVATE_KEY],
      chainId: 4
    },     
    goerli: {
      url: GOERLI_RPC_URL,
      accounts: [PRIVATE_KEY],
      chainId: 5
    },
    localhost: {
      url: 'http://127.0.0.1:8545/',
      chainId: 31337      
    },   
  },
  etherscan: {
    apiKey: ETHERSCAN_APIKEY
  },
  gasReporter: {
    enabled: true,
    outputFile: "gas-report.txt",
    noColors: true    
  },
  //This throws error
  namedAccounts: {
    deployer: {
      default: 0
    },
    player: {
      default: 1
    }
  },
}

export default config;

These are the package version's I'm using.

{
  "name": "sample-project",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers@^0.3.0-beta.13",
    "@nomiclabs/hardhat-etherscan": "^3.1.0",
    "@typechain/ethers-v5": "^10.1.0",
    "@typechain/hardhat": "^6.1.2",
    "@types/chai": "^4.3.3",
    "@types/node": "^18.7.15",
    "chai": "^4.3.6",
    "dotenv": "^16.0.2",
    "ethereum-waffle": "^3.4.4",
    "ethers": "^5.7.0",
    "hardhat": "^2.11.0",
    "hardhat-contract-sizer": "^2.6.1",
    "hardhat-deploy": "^0.11.13",
    "hardhat-gas-reporter": "^1.0.9",
    "prettier": "^2.7.1",
    "prettier-plugin-solidity": "^1.0.0-beta.24",
    "solhint": "^3.3.7",
    "solidity-coverage": "^0.8.0",
    "ts-node": "^10.9.1",
    "typechain": "^8.1.0",
    "typescript": "^4.8.2"
  },
  "dependencies": {
    "@chainlink/contracts": "^0.4.1"
  }
}

Best Answer

You need to import hardhat-deploy in order for the config type to be extended.

Example (es6):

import { HardhatUserConfig } from "hardhat/config";
import "hardhat-deploy"; // add this line

const config: HardhatUserConfig = {
  solidity: "0.8.9",
  namedAccounts: {
    deployer: { default: 0 },
  },
};

export default config;

See an example project with a complex config here.