DEV Community

Cover image for How to Setup Solidity Environment with Hardhat
Adetutu Gbangbola
Adetutu Gbangbola

Posted on

How to Setup Solidity Environment with Hardhat

Table Of Contents

    * [Chapter 1](Introduction)
    * [Chapter 2](Hardhat setup with Typescript)
    * [Chapter 3](Code sample with Hardhat)
    * [Chapter 4](Conclusion)
Enter fullscreen mode Exit fullscreen mode

Chapter 1: Introduction

Tooling around EVM based blockchain has improved drastically over the years, thanks to the unrelenting efforts of creators of tools like ganache, brownie, Hardhat etc.

In this tutorial, our main focused will be on hardhat, due to the large pool of Javascript developers and improving on the cons of previous tools, it has become the go-to tools for Ethereum Smart Contract Development.

Hardhat offers various features, including compiling, debugging, testing, and more. It can also be used for other EVM based development such as Binance Smart Chain,Polygon, Avalanche etc. This article will provide step-by-step guide on how to set up Hardhat with TypeScript, which can help you get started with building decentralized applications on the Ethereum Network.

Chapter 2: Hardhat Setup


Command Line Interface (CLI)
Code editor (e.g., VS Code)

There are different ways to set up Hardhat on your PC, and you can choose to install it globally. However, for this tutorial, we will install it locally, allowing us to have a version of Hardhat specific to this project that can be reused in different repositories or projects. In this tutorial, I will be using Yarn and the VS Code editor, but feel free to use any tools that suit you.

Steps to set up Hardhat locally:
  1. Create a new folder on your PC.
  2. Open your command line interface (CLI).
  3. Navigate to the folder you just created.
  4. Initialize the project using Yarn. In this tutorial, I will be using Yarn version 2. You start by typing the code below in your terminal.
  yarn init -2
Enter fullscreen mode Exit fullscreen mode

5.Install the required node modules.
If you're using Yarn version 2, you need to perform an additional configuration of setting up the node modules. If you were using npm, this step is not necessary because npm uses node modules by default. Use the following code to configure Yarn:

   yarn config set nodeLinker node-modules
Enter fullscreen mode Exit fullscreen mode

6.Install Hardhat as a development dependency.
Adding Hardhat as a dependency will populate the package.json file with all the necessary information for Hardhat. It will also install some packages in the project environment.

   yarn add hardhat --dev
Enter fullscreen mode Exit fullscreen mode

After successfully installing Hardhat, you will find it listed in your package.json file.

screenshot of actions 4, 5, and 6 executed in the command prompt (CMD)


7.Initialize Hardhat.
After installing Hardhat, the next step is to initialize it using the following code. The use of Yarn in the command is necessary because we installed it locally in this project, not globally on the machine.

   yarn hardhat init
Enter fullscreen mode Exit fullscreen mode

During the initialization process, I will select TypeScript as an option. However, you may choose JavaScript as there isn't much difference for this guide.

Additionally, while installing, Hardhat will suggest some dependencies for you to install. It's important to install them as they are required for the project. You can copy the entire line of code shown in your command line interface and paste it into your terminal. This will add all the necessary dependencies to your local project environment.

8.Compiling the Code
To compile your Hardhat code, simply run the following command:

  yarn hardhat compile
Enter fullscreen mode Exit fullscreen mode

After successful compilation, you will see the following output in your terminal:

Once compilation is complete, you should see the following directory structure in your code editor:

contracts: contains lock.sol,
scripts contain deploy.ts,
tests contains lock.ts, and
artifacts contains the ABI (Application Binary Interface) for your contracts.


Chapter 3: Code Sample with Hardhat

Now that we have successfully installed Hardhat, let's proceed to create a new contract named Hello.sol, which will execute a basic "Hello, World!" program.

Solidity Code

 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld {
    string public message;

    constructor() {
        message = "Hello, World!";

    function getMessage() public view returns (string memory) {
        return message;

Enter fullscreen mode Exit fullscreen mode

Typescript code

Here, I have created a result.ts script file for deploying our contract, as demonstrated above.

note: We're deploying locally only available on hardhat

  import { ethers } from "hardhat";

async function main() {

 const HelloWorld = await ethers.getContractFactory("HelloWorld");
 const helloWorld = await HelloWorld.deploy();

const resp = await helloWorld.getMessage();
console.log('Get message response', resp)

 .then(() => process.exit(0))
 .catch((error) => {

Enter fullscreen mode Exit fullscreen mode

To get the output of the code, run the following commands. The first command is used to compile the contract, and the second command is used to run the script with the title

yarn hardhat compile
yarn hardhat run scripts/result.ts
Enter fullscreen mode Exit fullscreen mode
screenshot of the code directory showing Hello.sol & result.ts



After deploying our contract, we can see the "Hello, World!" message displayed in the terminal.

For future Hardhat projects, you can use the project we created here as a starter file to avoid starting from scratch. Simply copy and paste the files into a new folder, excluding the node_modules folder to save copy time. Be sure to delete the existing files in the contracts, scripts, and tests folders before starting your new project.


In conclusion, By following this step-by-step instructions and harnessing the powerful features of Hardhat and TypeScript, developers can ensure a fantastic experience in Ethereum smart contract development. Enjoy coding and have a great time building!

Top comments (0)