6

I completely stuck on modules importing into Solidity contract. Here is my files/directory layout:

.
|-> contracts/
  |-> contract1.sol
  |-> contract2.sol

File contract1.sol contains only pragma and import statements like:

pragma solidity ^0.4.0;
import "./contract2" as c2;

File contract2.sol conntains only pragma statement:

pragma solidity ^0.4.0;

Then I open node shell and input following code:

> solc = require('solc')
> solc.compile(fs.readFileSync('contracts/contract1.sol').toString())
{ contracts: {},
  errors: [ ':3:1: ParserError: Source "contract2" not found: File not supplied initially.\nimport "./contract2" as c2;\n^-------------------------^\n' ],
  sourceList: [ '' ],
  sources: {} }

I already tried to:

  • specify full path in contract1.sol
  • add extension .sol
  • remove ./contracts/ prefix and run commands inside contracts folder.

Nothing works. Accordingly to this article begin import with dot and slash is enough but, damn, no!

How to include one sol file into another? Node version is 8.1.13, solcjs version is 0.4.13+commit.0fb4cb1a.Emscripten.clang

1 Answer 1

15

I deployed a contract using web3js sometime back and I faced the similar problem, I fixed it by defining all dependent contract in an input object and then compiled it.

You can take a look at this code for reference -

https://gist.github.com/inovizz/1fdc2af0182584b90008e0cf2895554c

Hope this helps.

3
  • 1
    Oh, man! If you'll be in Thailand let me know buy you a beer! You saved me from madness. Of course, it works. One question: could you give me recommendation, I just started to work with smart contracts, should I follow this way (I mean using web3 and other "low level" components) or better use Truffle?
    – Alex G.P.
    Jul 14, 2017 at 10:48
  • 3
    I prefer truffle, it makes life easier :D
    – Sanchit
    Jul 14, 2017 at 11:34
  • 1
    Truffle is very useful, but for starting with solidity I recommend you to use remix online IDE connected to a local node (just like testRPC); then set up MEW to run on your node for testing your contracts with external txs. Check this out for more info:myetherwallet.groovehq.com/knowledge_base/topics/… Or even this post: ethereum.stackexchange.com/questions/16740/… Jul 14, 2017 at 14:28

Your Answer

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

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