2

I know how to install solc via "npm install --save solc" then use solc.compile to compile one sol file. But now I've got A.sol file that imports B.sol file, and when I run node compile.js, no error in my terminal, but no json file is generated!

But when I use the same compile.js to compiile B.sol, it works. Why??? How to fix this importing feature in Solidity sol files??? Thank you.

3
const pathA = path.resolve(__dirname, 'contracts', 'A.sol');
//to find the path of A.sol inside the folder 'contract' in your project
const pathB = path.resolve(__dirname, 'contracts', 'B.sol');
const solA = fs.readFileSync(pathA, 'utf8');
const solB = fs.readFileSync(pathB, 'utf8');

const input = {
  sources: {
    'A.sol': solA,
    'B.sol': solB
  }
};
console.log(solc.compile(input, 1));
1
  • For those Googling, you may see an error to the effect of: Source "<contract>.sol" not found: File import callback not supported before implementing the fix.
    – soule
    Nov 18 '20 at 22:43
0

I have been unable to get solc js to import .sol files into eachother - I'm not sure if it's possible. My workaround is to just combine all .sol files into one single file and compile that.

See this question.

0

Add them as sources yes but also add a import callback if using solcjs.

https://github.com/ethereum/solc-js/issues/114#issuecomment-354752466

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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