Solidity Solc – How to Compile A.sol Importing B.sol

ethereumjimportnodejssolcsolidity

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.

Best Answer

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));