Solidity Compilation – Fix web3.eth.compile.solidity Not Compiling

compilationfrontendsolidityweb3js

I am trying to deploy a contract through my front-end via web3 API. However nothing appear to compile and there is no contract address or ABI to interact with the deployed contract (if it is deployed) and retrieve information (i.e. the string "petros"). I also tried the source code as a one line string.
Below is my code:

  var source = "" + 
    "contract demo {\n" +
    "   string public name = 'Petros';\n" +
    "   function changeName(string _newName){\n" +
    "     name = _newName;\n" +
    "   }\n" +
    "}\n"; 

var compiled = web3.eth.compile.solidity(source,
    function(err,res){ //necessary callback function
      console.log(JSON.stringify(res));
});

Best Answer

First check if you have installed the Solc : solc --version Then if your code won't work Try to use the following one instead:

 var source = 'contract demo {string public name = "Petros"; function changeName(string _newName){name = _newName; } }'; 

  var compiled = web3.eth.compile.solidity(source);

   console.log(compiled);