abhinayak
abhinayak

Reputation: 143

Code not compiling in nodejs,throws out an unexpected error(Web3.js)

I tried following this repo:-

But I am getting the following error on compiling the code with :-

code = fs.readFileSync('Voting.sol').toString()
solc = require('solc')
compiledCode = solc.compile(code)

It throws out this error:-

'{"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 after 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"}]}'

Upvotes: 9

Views: 3788

Answers (6)

Johnny Antony . P
Johnny Antony . P

Reputation: 89

I too faced with the same problem. Download and check for the correct version of solc version is installed or else it gonna give you the error.I did not specify the version to download,it downloaded the default version.

npm install --save [email protected] (specify the version)

"'{"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 after 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"}]}'"

Upvotes: 2

WongJoe
WongJoe

Reputation: 81

The version of solc and your contract should be the same.

For example, if your contract uses pragma solidity ^0.4.18; then you should install the same version of solc Using npm install [email protected].

Upvotes: 8

orangejuicejones
orangejuicejones

Reputation: 121

I found that if you put your input info into the JSON format per the solidity docs, then you are good regardless of the compiler. Before compiling "stringify" the file (JSON.stringify). After the file is compiled the object will be in string form, so then you may want to parse it (JSON.parse) to work with it from there. Here is a code sample with a console.log() of the contract in JSON form so you can see what you are working with.

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


const inboxPath = path.resolve(__dirname, 'contracts', 'inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');

var solcInput = {
    language: "Solidity",
    sources: { 
        contract: {
            content: source
        }
     },
    settings: {
        optimizer: {
            enabled: true
        },
        evmVersion: "byzantium",
        outputSelection: {
            "*": {
              "": [
                "legacyAST",
                "ast"
              ],
              "*": [
                "abi",
                "evm.bytecode.object",
                "evm.bytecode.sourceMap",
                "evm.deployedBytecode.object",
                "evm.deployedBytecode.sourceMap",
                "evm.gasEstimates"
              ]
            },
        }
    }
};

solcInput = JSON.stringify(solcInput);
var contractObject = solc.compile(solcInput);
contractObject = JSON.parse(contractObject);

console.log(contractObject);

Upvotes: 12

Banana Cake
Banana Cake

Reputation: 1242

I found another solution. In my case the problem was that the File.sol was raw solidity smart contract, but you need to put Compiler Standard Input JSON into solc.compile(). This works for Solidity 0.5.1. In this thread is my whole process step by step until the transaction sending.

Upvotes: 0

abhinayak
abhinayak

Reputation: 143

I found out the answer it was because of the npm version conflicts. Make sure that you have the right version of solc.Refer this repo

Upvotes: 0

bereket gebredingle
bereket gebredingle

Reputation: 12996

You have to specify what type of encoding is used for the solidity file.

code = fs.readFileSync('Voting.sol', 'utf8');

and you have to specifiy the number of contracts your are about to compile.

compiledCode = solc.compile(code, 1);

Upvotes: 0

Related Questions