Skip to content

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.

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:

test/remappings.txt
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.

To learn more about remappings in Hardhat 3, read the remappings guide.