React Project Setup Using Hardhat & Truffle — part 1

Shih-Yu Hwang
Coinmonks
6 min readAug 23, 2021

--

Photo by Jeremy Bishop on Unsplash

When developing smart contracts, it is important to use a development environment. This article will explore Truffle and Hardhat side by side to translate their commands and how to adapt from one to the other by creating a small project in React.

What is Truffle and Hardhat?

Truffle and Hardhat are both a development environment for writing smart contracts on the Ethereum blockchain. These tools allows developers to:

  • Compile contracts
  • Test contracts
  • Deploy contracts
  • Debug contracts

You can learn more about each here:

Truffle

Truffle logo
Truffle

HardHat

Hardhat

Project Setup

The first step is to set up a project with React before we start writing our smart contracts.

  • Go to the folder where you want the project to live

Truffle Setup Method

The output should look something like this:

Then move into that folder you just created

Another option is using Truffle boxes, there is one created with React that you can use. To learn more about Truffle boxes, go here Truffle Boxes

If you don’t have truffle installed globally

If you have truffle installed globally

You should get a similar output like below:

Now let’s take a look from Hardhat’s side

Hardhat Setup Method

Since hardhat is a lighter tool that utilizes more on plugins, it can be installed as a dependency in your project.

Now we should see something like this:

Then move into that folder you just created

Alright! We just went through the process of adding Truffle/Hardhat into our React project, next we will initialize them.

Installation

Now that the folders are setup, let’s install the development tools

Truffle Installation Method

In the project folder you just created, install truffle.

This is only if you didn’t use truffle unbox! If you use truffle unbox you can skip this step since the unboxing executes this step.

and you should get a output similar to the following:

Hardhat Installation Method

Go into the project folder and install hardhat as a dependency for the project

Now install hardhat

You should get this in the terminal:

Let’s select:

Then set your project root directory:

Since React already has a .gitingnore file, select no

Since hardhat relies on other dependencies, we will need to install them, however for the purpose of keeping the comparison as close as possible, we will install them ourselves

Hardhat usually use ethers.js but since truffle uses web3.js, we will use web3.js with hardhat as well

You might see some warnings during this installation but it should be fine for now just as long as you get a project created confirmation

Hardhat Installing Dependencies

We will need web3.js to interact with the blockchain in our project later on

This lets hardhat interact with web3.js library

This lets hardhat interact with web3.js library

We have now know how to completely set up and install a React project with both Truffle and Hardhat

In part 2 we will configure both the development environment and start writing a contract! Click here for part 2.

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--