Importing & Loading Source Files in Solidity¶
There are two main reasons for loading external files into Remix:
- to import a library or dependency (for files you will NOT be editing)
- to load some files for manipulation, editing and play (for files you might want to edit)
Importing a library or dependency¶
When importing from NPM, or a URL (like github, a IPFS gateway, or a Swarm gateway) you do not need to do anything more than use the
import statement in your contract. The dependencies do not need to be « preloaded » into the File Explorer”s current Workspace before the contract is compiled.
Files loaded from the import statement are placed in the Files Explorer”s current Workspace”s
Under the hood, Remix checks to see if the files are already loaded in the .deps directory. If not, it gets them via unpkg if it is an NPM lib.
Here are some example import statements:
Import from NPM¶
Note: In the example above, @openzeppelin is the name of the npm library. In the following example the library”s name does not begin with an @ - but Remix will go and check npm for a library of that name.
Import from a Github URL¶
You should specify the release tag (where available), otherwise you will get the latest code in the master branch. For OpenZeppelin Contracts you should only use code published in an official release, the example above imports from OpenZeppelin Contracts v2.5.0.
Import from Swarm¶
Import from IPFS¶
Importing a local file not in .deps¶
To import a file NOT in the .deps folder, use a relative path (./). For example:
Note: It is not possible to import across Workspaces.
Importing a file from your computer”s filesystem¶
This method uses remixd - the remix daemon. Please go to the remixd docs for instructions about how to bridge the divide between the browser and your computers filesystem.
Importing a files for manipulation¶
When importing from the home tab widgets or with a remix command in the console, the files are placed in the root of the current Workspace inside a folder the shows their source - eg github or gists.
Loading with a remix command in the console¶
The 2 remix commands for loading are:
Accessing files loaded from the Home tab or from a remix command¶
When you load from github, a folder named
github folder is created in the root of your current workspace. To import a file from the
github folder, you would use a command like this:
Notice that this import statement doesn”t include the version information that was in the remix.load(url) command. So it is recommended that you use the methods described at the top of this page for importing dependencies that you are not intending to edit.
Assume the .sol file that contained the import statement above is in the contracts folder. Notice that this import statement didn”t need to traverse back to the github folder with a relative path like: ../github.