How can they be used with solc?

1 Answer 1


You may want to take a look at the docs on Layout of a Solidity Source File.

The solidity compiler can only reference files that exist locally on your computer. So importing directly from GitHub (as an example) is not possible.

Now, let's imagine you want to use OpenZeppelin's amazing library of smart contracts, @openzeppelin/contracts-ethereum-package:

pragma solidity 0.5.11;

import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";

contract MyContract {
    using SafeMath for uint256;

If you're using truffle, this is easy-peasy. You just run truffle compile and the imports are magically sorted out for you.

If you want to use solc, things get a bit more complicated. You have to specify the following:

  • "prefix" = the path that's used in your smart contract, i.e. "@openzeppelin/contracts-ethereum-package"
  • "target" = the absolute path of OpenZeppelin's contracts downloaded on your computer

The format looks like this:

solc prefix=target ./MyContract.sol

Let's plug & play the paths:

solc --bin @openzeppelin/contracts-ethereum-package=/Your/Absolute/Path/To/@openzeppelin/contracts-ethereum-package ./MyContract.sol

(I specified --bin so that the compiler will print the bytecode in your terminal)

If you downloaded the OpenZeppelin library via npm, it should be located in the "node_modules" folder at the root of your repo.


  • If you don't have the solc compiler, check out the docs to ensure you get it right for your operating system.
  • Do NOT use npm/node to install the compiler, since it has less features and remappings don't work.
  • Different tools may treat remappings differently. At the time of writing this answer, [email protected] was generating a different data area at the end of the contract bytecode than [email protected].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.