1

I am using node@10.14.2 and solc@0.5.4 to compile solidity files using the following code:

var solc = require('solc');
var input = {
  language: 'Solidity',
  sources: {
    'Bon.sol':{
      content: 'pragma solidity ^0.5.2;  contract Bon { string private _name = "thunderbolt"; function getName() public view returns(string memory){  return _name;}}'
    }
  }
}
var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output);

The output of the above command is:

{ sources: { 'Bon.sol': { id: 0 } } }

I cannot find the abi, bytecode and other properties as mentioned here. Please help.

I missed one key(settings) for the input (i thought it was default and wasn't required). Now the working code:

var solc = require('solc');
var fs = require('fs');
var path = require('path');

var contractsBasePath = path.join(__dirname,'..','contracts');

var input = {
  language: 'Solidity',
  sources: {
    'Bon.sol':{
      content: 'pragma solidity ^0.5.2;  contract Bon { string private _name = "thunderbolt"; function getName() public view returns(string memory){  return _name;}}'
    }
  },
  settings: { // This is the one that i had missed
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}


var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output); // #

module.exports = output;

Output of line // #

{ contracts: { 'Bon.sol': { Bon: [Object] } },
  sources: { 'Bon.sol': { id: 0 } } }
{ 'Bon.sol':
   { Bon:
      { abi: [Array],
        devdoc: [Object],
        evm: [Object],
        metadata:
         '{"compiler":{"version":"0.5.4+commit.9549d8ff"},"language":"Solidity","output":{"abi":[{"constant":true,"inputs":[],"name":"getName","outputs":[{"name":"","type":"string"}],"payable":f
alse,"stateMutability":"view","type":"function"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"Bon.sol":"Bon"},"evmVersion":"byzantium","libraries":{},"opt
imizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"Bon.sol":{"keccak256":"0xe1a58ef2240aa0a944fd607dedf8a8f0f2c14d83e83b0c14c4f07122ee48a1bf","urls":["bzzr://41933840c9e8b838824af

ebf33d9cc729be25f2785812b2c15e9a1352b2df6fb"]}},"version":1}',
        userdoc: [Object] } } }
0

I missed one key(settings) for the input (i thought it was default and wasn't required). Now the working code:

var solc = require('solc');
var fs = require('fs');
var path = require('path');

var contractsBasePath = path.join(__dirname,'..','contracts');

var input = {
  language: 'Solidity',
  sources: {
    'Bon.sol':{
      content: 'pragma solidity ^0.5.2;  contract Bon { string private _name = "thunderbolt"; function getName() public view returns(string memory){  return _name;}}'
    }
  },
  settings: { // This is the one that i had missed
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}


var output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log(output); // #

module.exports = output;

Output of line // #

{ contracts: { 'Bon.sol': { Bon: [Object] } },
  sources: { 'Bon.sol': { id: 0 } } }
{ 'Bon.sol':
   { Bon:
      { abi: [Array],
        devdoc: [Object],
        evm: [Object],
        metadata:
         '{"compiler":{"version":"0.5.4+commit.9549d8ff"},"language":"Solidity","output":{"abi":[{"constant":true,"inputs":[],"name":"getName","outputs":[{"name":"","type":"string"}],"payable":f
alse,"stateMutability":"view","type":"function"}],"devdoc":{"methods":{}},"userdoc":{"methods":{}}},"settings":{"compilationTarget":{"Bon.sol":"Bon"},"evmVersion":"byzantium","libraries":{},"opt
imizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"Bon.sol":{"keccak256":"0xe1a58ef2240aa0a944fd607dedf8a8f0f2c14d83e83b0c14c4f07122ee48a1bf","urls":["bzzr://41933840c9e8b838824af

ebf33d9cc729be25f2785812b2c15e9a1352b2df6fb"]}},"version":1}',
        userdoc: [Object] } } }

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