Configuration de Hardhat
Squelette du projet
Configuration de Hardhat est une leçon Web3 & DApp Development Fundamentals gratuite sur CoddyKit. Ceci est la leçon 1 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Web3 & DApp Development Fundamentals, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
What Is Hardhat
Hardhat is a development environment for Ethereum that helps you compile, test, deploy, and debug smart contracts.
- Written in JavaScript/TypeScript and run with Node.js.
- Ships with a built-in local Ethereum network.
- Has a rich plugin ecosystem (ethers, waffle, gas reporter).
It is the most popular alternative to Truffle and Foundry for JS-centric teams.
Prerequisites
Before installing Hardhat you need Node.js (LTS recommended) and a package manager such as npm or yarn.
Check your versions to make sure the toolchain is ready:
node -v
npm -vHardhat targets Node.js LTS releases, so avoid odd-numbered experimental versions.
node -v
npm -vCreating a Project Folder
Each Hardhat project lives in its own directory with a local package.json. Start by creating and initializing the folder:
mkdir my-dapp
cd my-dapp
npm init -yThis package.json will track Hardhat and your other dev dependencies.
mkdir my-dapp
cd my-dapp
npm init -yInstalling Hardhat
Install Hardhat as a dev dependency so it is not bundled into production:
npm install --save-dev hardhatInstalling locally (not globally) pins the exact version per project, which keeps builds reproducible across machines and CI.
npm install --save-dev hardhatScaffolding a Project
Run the Hardhat init wizard to scaffold a starter project:
npx hardhat initYou can choose a JavaScript or TypeScript project, or an empty config. The wizard creates sample contracts, tests, and a config file.
npx hardhat initProject Structure
A typical Hardhat project layout looks like this:
contracts/— your Solidity source files.scripts/— deployment and automation scripts.test/— Mocha/Chai test files.hardhat.config.js— central configuration.artifacts/andcache/— generated build output.
The Config File
The hardhat.config.js file is the heart of your setup. A minimal config just exports the Solidity version:
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.24",
};You will later extend it with networks, plugins, and compiler settings.
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: "0.8.24",
};The Hardhat Toolbox
The Hardhat Toolbox bundles the most common plugins in one package:
- ethers.js integration.
- Chai matchers for assertions.
- Gas reporter and coverage.
- Etherscan verification.
npm install --save-dev @nomicfoundation/hardhat-toolboxnpm install --save-dev @nomicfoundation/hardhat-toolboxRunning Hardhat Tasks
Hardhat exposes its functionality through tasks. List all available tasks with:
npx hardhatCommon built-in tasks include compile, test, node, and clean. You always run them via npx to use the local install.
npx hardhatA First Contract
Add a simple contract under contracts/Greeter.sol:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Greeter {
string public greeting = "Hello, Hardhat";
}The public state variable automatically gets a getter you can call after deployment.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
contract Greeter {
string public greeting = "Hello, Hardhat";
}Git and Ignore Rules
Generated folders should not be committed. Create a .gitignore:
node_modules
artifacts
cache
.envKeeping .env ignored protects secrets like private keys and RPC URLs from leaking into version control.
node_modules
artifacts
cache
.envQuick Check
Test your understanding of Hardhat project setup.
Recap
You set up a Hardhat project from scratch.
- Hardhat is a Node.js-based Ethereum dev environment.
npx hardhat initscaffolds contracts, scripts, tests, and config.hardhat.config.jsdefines Solidity version, networks, and plugins.- The Toolbox bundles ethers, Chai matchers, gas reporting, and verification.
- Ignore
node_modules,artifacts,cache, and.envin Git.
Questions Fréquemment Posées
La leçon « Configuration de Hardhat » est-elle gratuite ?
Oui — le texte complet de « Configuration de Hardhat » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Web3 & DApp Development Fundamentals, passe à CoddyKit PRO. Le cours Web3 & DApp Development Fundamentals comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Configuration de Hardhat » ?
Squelette du projet Tu pratiques Web3 & DApp Development Fundamentals avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Web3 & DApp Development Fundamentals ?
Aucune expérience préalable n'est requise. Web3 & DApp Development Fundamentals sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 1 sur 4.
Combien de temps prend la leçon « Configuration de Hardhat » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Web3 & DApp Development Fundamentals ?
Oui. Chaque leçon Web3 & DApp Development Fundamentals inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Configuration de Hardhat
- Compiler des contrats
- Scripts et tâches
- Réseau local et fork