2

I am able to successfully compile a smart contract in remix IDE. However when I try to do the same with solc npm command, it shows a JSON error when compiling.

Voting.sol

pragma solidity ^0.4.25;
contract Voting{
    mapping(bytes32=>uint8) public votesReceived;

    function totalVotesFor(bytes32 candidate) view public returns (uint8){
        return votesReceived[candidate];
    }

    function voteForCandaidate(bytes32 candidate)public{
        votesReceived[candidate]+=1;
    }

 }

compile.js

var fs = require('fs');
var solc = require('solc');
var code = fs.readFileSync('Voting.sol').toString();
console.log(code);
console.log(solc.compile(code));

REMIX IDE is compiling the code without any errors

Compilation error when running the same code with solc

{"errors":[{"component":"general","formattedMessage":"* Line 1, Column 1\n  Syntax error: value, object or array expected.\n* Line 1, Column 2\n  Extra non-whitespace afte
r JSON value.\n","message":"* Line 1, Column 1\n  Syntax error: value, object or array expected.\n* Line 1, Column 2\n  Extra non-whitespace after JSON value.\n","severity
":"error","type":"JSONError"}]}
  • What do you get when you run solc --version? Is it 0.4.25 or better? – Rob Hitchens - B9lab Nov 21 '18 at 20:21
  • You have installed solc v0.5.0 wich uses a different syntaxis. You can force installation of the previous version with npm install solc@0.4.25. – Ismael Nov 23 '18 at 0:26
  • Here is an example stackoverflow.com/questions/49709000/…, how solc must be used since v0.5.0. Or you can use one of the lowlevel functions. – Ismael Nov 23 '18 at 0:38
1

Try to change your code like this:

let source = fs.readFileSync(contractPath, 'UTF-8');
let compilationData = solc.compile(source, 1).contracts[':'+ contractName];

Make sure you have installed same version of solc as mentioned in your contract pragma.

  • 1
    this helped me to better my understanding – Satya Dec 1 '18 at 11:01

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.