Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Setting Up Your Node.js & Hardhat Development Environment

Hello and welcome to the first lesson of Module 2!

In the previous module, you built a solid foundation by learning the core concepts of Ethereum. You went from understanding the theory to setting up a wallet, acquiring test funds, and exploring the blockchain as a user. Now, we shift from being a user to a developer.

This lesson kicks off your journey into building on Ethereum. Our learning outcome is to install and configure a professional development environment with Node.js and Hardhat. We will set up the essential tools that you will use throughout the rest of this course to write, test, and deploy your own cryptocurrency token.

As a backend developer, you're familiar with setting up development environments. The process today will be analogous to setting up a project with tools like Express.js for Node.js or Django for Python. Here, Node.js provides the runtime, and Hardhat serves as the specialized framework for all things related to Ethereum smart contracts.

Part 1: Installing the Foundation - Node.js

Hardhat and most of the Ethereum development ecosystem are built using JavaScript and TypeScript. Therefore, the first requirement is to have Node.js installed, which is a JavaScript runtime environment.

Given your backend development experience, especially with TypeScript, you likely already have Node.js and its package manager, npm, installed. Let's verify that first. Open your terminal or command prompt and run the following commands:

node -v
npm -v

Hardhat requires a recent version of Node.js (version 18.0 or higher). If you don't have Node.js installed, or if your version is older than 18.0, you will need to install or upgrade it.

Setting up the environment

The official Hardhat documentation provides excellent, OS-specific instructions for installing or upgrading Node.js. Follow the guide for your operating system.

Please read the section 'Installing Node.js'. If you are on Windows, we strongly recommend following the instructions to use Windows Subsystem for Linux (WSL 2), as it will provide a smoother development experience. If you already have Node.js but need to upgrade, follow the instructions in the 'Upgrading your Node.js installation' section.

A Note on Your Code Editor

While you can use any text editor, we recommend Visual Studio Code (VS Code) for Solidity development. If you use VS Code, consider installing the official Hardhat for Visual Studio Code extension, which provides excellent Solidity language support, including syntax highlighting and code completion.

Part 2: What is Hardhat?

With Node.js in place, let's introduce our primary tool: Hardhat.

Hardhat is a flexible and extensible development environment for Ethereum. It streamlines the entire development lifecycle of a smart contract. Think of it as a complete workshop for building, testing, and deploying your blockchain applications. Throughout this course, you will use Hardhat to:

  • Compile your Solidity code into bytecode that the Ethereum Virtual Machine (EVM) can execute.
  • Run a local Ethereum network on your machine for fast and easy development and testing, without needing a real testnet.
  • Write and run automated tests for your smart contracts to ensure they are secure and function correctly.
  • Deploy your contracts to Ethereum testnets (like Sepolia) and eventually to the mainnet.
  • Debug transactions and inspect the state of the blockchain.

Part 3: Creating Your First Hardhat Project

Now, let's get our hands dirty and set up a Hardhat project. This process involves creating a project folder, initializing it as a Node.js project, and then installing and running Hardhat.

  1. Create a project folder: Open your terminal and create a new directory for your project.

    mkdir my-token-project
    cd my-token-project
    
  2. Initialize a Node.js project: This command creates a package.json file, which will track your project's dependencies, including Hardhat. The -y flag accepts all the default prompts.

    npm init -y
    
  3. Install Hardhat: We install Hardhat as a "dev dependency" because it's a tool we use for development, not one that runs in production.

    npm install --save-dev hardhat
    

    Your terminal output should look similar to this:

    Hardhat Installation Output
    A terminal showing the successful completion of the npm install --save-dev hardhat command. It lists the number of packages added and audited.

  4. Initialize the Hardhat project: Now, run the Hardhat command-line interface using npx, which executes npm packages.

    npx hardhat
    

    This will launch an interactive setup guide.

The following video from Patrick Collins provides a clear walkthrough of the Hardhat installation and initialization process.

Build and Deploy Smart Contracts using Hardhat | [See Description]

Watch this segment to see the Hardhat installation and project initialization steps in action. Pay close attention to the interactive prompts after running npx hardhat.

Watch from 01:16 to 02:47. During the interactive setup, select 'Create a JavaScript project' (or 'Create a basic sample project' in older versions) and agree to the defaults for the project root and adding a .gitignore file. When prompted to install additional dependencies (@nomicfoundation/hardhat-toolbox), say yes.

Part 4: Project Structure and Verification

After the initialization is complete, Hardhat generates a sample project with a standard folder structure. Understanding this structure is key to organizing your work.

Here are the most important files and directories:

  • contracts/: This is where you will write your Solidity smart contract source files (files ending in .sol).
  • scripts/: This folder is for scripts that automate tasks and interact with your contracts, such as a deployment script.
  • test/: Contains the automated tests for your smart contracts. Writing robust tests is crucial in blockchain development.
  • hardhat.config.js: The main configuration file for your project. Here you'll define settings like the Solidity compiler version and network details (which we'll explore in the next lesson).
  • package.json: Tracks all your project's Node.js dependencies.

The next segment of the video you just watched provides a quick tour of this structure.

Build and Deploy Smart Contracts using Hardhat | [See Description]

Let's continue with the same video for a brief explanation of the newly created files and folders.

Watch from 02:59 to 04:13 to get a visual overview of the project structure.

Verifying Your Setup

The final and most important step is to verify that everything is working correctly. Hardhat provides two simple commands for this.

  1. Compile the sample contract:

    npx hardhat compile
    

    This command looks for .sol files in the contracts/ directory, compiles them, and if successful, places the resulting artifacts (like the ABI and bytecode) in a new artifacts/ directory.

  2. Run the sample tests:

    npx hardhat test
    

    This command executes the test file in the test/ directory. The sample test will spin up a temporary in-memory blockchain, deploy the sample contract, and check if its functions work as expected.

The video below demonstrates these two verification steps. A successful output confirms your environment is ready for development.

Web3 Course | #2 Setting up your Dev Environment with Node.js + Hardhat

This clip from ABTube shows the compilation and testing commands being run successfully. Your output should look very similar.

Watch from 00:13:13 to 00:15:23. Observe the output of both the npx hardhat compile and npx hardhat test commands.

If both commands complete without errors, congratulations! You have successfully set up a professional Ethereum development environment.

Test your understanding!

Imagine you've written a new utility script in your project that interacts with your deployed smart contract to check its total supply. According to the standard Hardhat project structure, in which directory should you save this new script file?

Show answer

You should place the script in the scripts/ directory. This folder is intended for standalone scripts that automate workflows or interact with your contracts.

Conclusion

In this lesson, you've made a significant leap from being an Ethereum user to an Ethereum developer. You have set up a complete, professional-grade local development environment, which is the standard for building secure and robust smart contracts.

Key Takeaways:

  • Ethereum development tools like Hardhat are built on Node.js, making it a foundational prerequisite.
  • Hardhat is a powerful framework that handles compiling, testing, deploying, and debugging smart contracts.
  • A standard Hardhat project is initialized with npx hardhat and has a clear structure with dedicated folders for contracts, scripts, and tests.
  • The npx hardhat compile and npx hardhat test commands are essential for verifying your setup and your code.

Preview of the Next Lesson:
You've now created a basic, functional Hardhat project. In the next lesson, we will initialize and configure a Hardhat project for compiling and deploying smart contracts. We will take a deep dive into the hardhat.config.js file, where you'll learn to configure the Solidity compiler and set up network connections, preparing you to deploy contracts to a live testnet like Sepolia.

Can't find a good explanation? Sign up and we'll make it for you

Sign up