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.

4 Answers 4

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));
  • 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
    Commented Nov 18, 2020 at 22:43

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



I am using @remix-project/remix-solidity on browser, it works great for me

Simply call:

const response = await compile(
                    [CONTRACT_FILE_NAME]: {
                        content: source
                , {
                    version: SOLIDITY_COMPILER_VERSION
                }, handleNpmImport) as CompilerAbstract;

Source code : https://github.com/vanduc1102/samples/blob/master/reactjs/cra-solidity-etherjs/package.json


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.

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.