How to setup hardhat environment?

A guide to setting up your Hardhat development envirnmet.

ยท

3 min read

Step 1

Hardhat is a development environment for Ethereum smart contracts. To set it up, follow these steps:

  1. Install Node.js and npm: Ensure you have Node.js and npm installed on your machine. You can download them from nodejs.org.

  2. Initialize a new Node.js project: Open a terminal and run these commands.

     mkdir Hello-World  //for make diretory
     cd Hello-World    // initialize folder
     npm init --yes   // npm in
    

    You will get package.json file after these three commands.

  3. Install Hardhat: Run this to install Hardhat as a development dependency.

     npm install --save-dev hardhat
    

    If you have followed the instructions properly, and everything went according to the plan, you have HardHat installed. ๐ŸŽ‰

    Also package-lock.json file created.

  4. Initialize Hardhat: Run npx hardhat to initialize the Hardhat project. Follow the prompts to create a hardhat.config.js file.

     npx hardhat
    

    You will see something like this:

  5. Create a JavaScript project: Just follow through the questions and answer them yes. For me it was something like below:

  6. Hardhat ether: You need to install some more dependencies. Hardhat ethers is a plugin that gives us access to ethers.js library. ethers.js library gives us a way to interact with the blockchain world, we can deploy contracts, we can fetch contracts, we can call their functions, all thanks to this library.

     npm install --save-dev @nomiclabs/hardhat-ethers ethers --force
    

    Lastly, run the following command on safe side to ensure that all required installations are done:

     npm install @nomiclabs/hardhat-ethers ethers hardhat @nomicfoundation/hardhat-toolbox --force
    
  7. Install additional plugins (optional): Depending on your needs, you might want to install additional plugins. For example, you can install this plugin for Ethereum testing:

    npm install --save-dev @nomiclabs/hardhat-waffle

    Remember to update your hardhat.config.js file to include these plugins.

  8. Project Structre:

    Side tip : Install Solidity Extension by Juan Blanco in VScode to help with syntax and coding error issues.

  9. Write your smart contracts: Create your Ethereum smart contracts in the contracts/ directory.

  10. Run tests (optional): If you installed the testing plugin, write tests in the test/ directory and run them using npx hardhat test.

  11. Compile your contracts: Run npx hardhat compile to compile your smart contracts. The compiled artifacts will be stored in the artifacts/ directory.

  12. Configure network settings: Edit your hardhat.config.js file to configure Ethereum network settings, such as the network to deploy to.

  13. Deploy contracts (optional): If you want to deploy your contracts, you can do so using npx hardhat run scripts/deploy.js.

  14. Interact with your contracts: Use the Hardhat console or write scripts to interact with your deployed contracts.

That's a basic setup for Hardhat. Customize it based on your project requirements and explore the documentation for more advanced features.

ย