Using absolute imports in Solidity
By default, Hardhat 3 supports relative imports to local files. For example, if you have this project:
Directorymy-project/
- package.json
- hardhat.config.ts
Directorycontracts/
- Foo.sol
Directorytest/
- Foo.t.sol
The test test/Foo.t.sol would import Foo.sol using "../contracts/Foo.sol". We recommend using this style of imports because it’s compatible with every tool.
Using remappings for absolute imports
Section titled “Using remappings for absolute imports”If you want to use absolute imports and write "contracts/Foo.sol" instead, you can use a remapping.
To do this, add a remappings.txt file to the directory where you want to use absolute imports. For example, if you only want them for test files, add this:
contracts/=../contracts/By adding the remappings.txt to the directory where it’s needed, you avoid polluting other source files and keep your project compatible with more tools.
Learn more
Section titled “Learn more”To learn more about remappings in Hardhat 3, read the remappings guide.