Import files in Solidity

Solidity supports import statements similar to those of other programming languages like JavaScript. This helps to keep the source code modular and easier to maintain. However, default exports are not possible in Solidity.

A simple import statement uses the import statement and the filename of the imported file:

import "filename";

import from github

It is also possible to import directly from github, in this example we import the OpenZeppelin SafeMath library for Solidity versions below 0.8: import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/release-v3.4/contracts/math/SafeMath.sol"; When importing files from github and other sources, it is important to check the version pragma to make sure, the imported file is compatible to the compiler version used.

aliases and namespaces

It is recommended to create a new global symbol for imports to keep the namespace clean. import * as newSymbol from "filename"; All global symbols are then available by using newSymbol.symbol syntax. It is also possible to rename symbols using aliases to prevent naming collisions. To use aliases in Solidity the as keyword is used. The following code makes old_symbol as new_symbol available. import {old_symbol as new_symbol, symbol2} from "filename";


In case an imported identifier for variables, functions, or contracts has the same name a DeclarationError is raised by the compiler. In the same way, it would be raised by using an identifier twice within the same file. DeclarationError: Identifier already declared



Click to jump to section