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












2















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









share|improve this question

























  • You better go on the repo to open an issue there. (I assume you already checked the opened issues)

    – Izio
    Nov 21 '18 at 9:36











  • Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

    – abhinayak
    Nov 21 '18 at 9:45











  • What is the code variable? If it's some sol file then how did you read it?

    – Molda
    Nov 21 '18 at 9:51











  • Also since the compilation is done by Solidity why don't you google google.com/….

    – Molda
    Nov 21 '18 at 9:52








  • 1





    The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

    – Molda
    Nov 23 '18 at 8:56


















2















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









share|improve this question

























  • You better go on the repo to open an issue there. (I assume you already checked the opened issues)

    – Izio
    Nov 21 '18 at 9:36











  • Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

    – abhinayak
    Nov 21 '18 at 9:45











  • What is the code variable? If it's some sol file then how did you read it?

    – Molda
    Nov 21 '18 at 9:51











  • Also since the compilation is done by Solidity why don't you google google.com/….

    – Molda
    Nov 21 '18 at 9:52








  • 1





    The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

    – Molda
    Nov 23 '18 at 8:56
















2












2








2


1






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









share|improve this question
















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






node.js blockchain solidity web3js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:44







abhinayak

















asked Nov 21 '18 at 9:13









abhinayakabhinayak

237




237













  • You better go on the repo to open an issue there. (I assume you already checked the opened issues)

    – Izio
    Nov 21 '18 at 9:36











  • Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

    – abhinayak
    Nov 21 '18 at 9:45











  • What is the code variable? If it's some sol file then how did you read it?

    – Molda
    Nov 21 '18 at 9:51











  • Also since the compilation is done by Solidity why don't you google google.com/….

    – Molda
    Nov 21 '18 at 9:52








  • 1





    The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

    – Molda
    Nov 23 '18 at 8:56





















  • You better go on the repo to open an issue there. (I assume you already checked the opened issues)

    – Izio
    Nov 21 '18 at 9:36











  • Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

    – abhinayak
    Nov 21 '18 at 9:45











  • What is the code variable? If it's some sol file then how did you read it?

    – Molda
    Nov 21 '18 at 9:51











  • Also since the compilation is done by Solidity why don't you google google.com/….

    – Molda
    Nov 21 '18 at 9:52








  • 1





    The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

    – Molda
    Nov 23 '18 at 8:56



















You better go on the repo to open an issue there. (I assume you already checked the opened issues)

– Izio
Nov 21 '18 at 9:36





You better go on the repo to open an issue there. (I assume you already checked the opened issues)

– Izio
Nov 21 '18 at 9:36













Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

– abhinayak
Nov 21 '18 at 9:45





Hey @Izio , i did open an issue but looks like the developer is inactive for few days. A fast help will be appreciated. Thanks

– abhinayak
Nov 21 '18 at 9:45













What is the code variable? If it's some sol file then how did you read it?

– Molda
Nov 21 '18 at 9:51





What is the code variable? If it's some sol file then how did you read it?

– Molda
Nov 21 '18 at 9:51













Also since the compilation is done by Solidity why don't you google google.com/….

– Molda
Nov 21 '18 at 9:52







Also since the compilation is done by Solidity why don't you google google.com/….

– Molda
Nov 21 '18 at 9:52






1




1





The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

– Molda
Nov 23 '18 at 8:56







The compile function takes different arguments depending on which version you have. Make sure to check documentation github.com/ethereum/solc-js#readme

– Molda
Nov 23 '18 at 8:56














4 Answers
4






active

oldest

votes


















0














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);





share|improve this answer



















  • 1





    specifing the number of contract, gives out an AssertionError

    – abhinayak
    Nov 21 '18 at 10:22



















0














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






share|improve this answer































    0














    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.






    share|improve this answer































      0














      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);





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408632%2fcode-not-compiling-in-nodejs-throws-out-an-unexpected-errorweb3-js%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        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);





        share|improve this answer



















        • 1





          specifing the number of contract, gives out an AssertionError

          – abhinayak
          Nov 21 '18 at 10:22
















        0














        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);





        share|improve this answer



















        • 1





          specifing the number of contract, gives out an AssertionError

          – abhinayak
          Nov 21 '18 at 10:22














        0












        0








        0







        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);





        share|improve this answer













        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);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 10:09









        bereket gebredinglebereket gebredingle

        1,166618




        1,166618








        • 1





          specifing the number of contract, gives out an AssertionError

          – abhinayak
          Nov 21 '18 at 10:22














        • 1





          specifing the number of contract, gives out an AssertionError

          – abhinayak
          Nov 21 '18 at 10:22








        1




        1





        specifing the number of contract, gives out an AssertionError

        – abhinayak
        Nov 21 '18 at 10:22





        specifing the number of contract, gives out an AssertionError

        – abhinayak
        Nov 21 '18 at 10:22













        0














        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






        share|improve this answer




























          0














          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






          share|improve this answer


























            0












            0








            0







            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






            share|improve this answer













            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







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 2 '18 at 5:26









            abhinayakabhinayak

            237




            237























                0














                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.






                share|improve this answer




























                  0














                  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.






                  share|improve this answer


























                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 16 '18 at 17:43









                    Banana CakeBanana Cake

                    949




                    949























                        0














                        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);





                        share|improve this answer




























                          0














                          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);





                          share|improve this answer


























                            0












                            0








                            0







                            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);





                            share|improve this answer













                            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);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Feb 23 at 21:41









                            orangejuicejonesorangejuicejones

                            1




                            1






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408632%2fcode-not-compiling-in-nodejs-throws-out-an-unexpected-errorweb3-js%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Xamarin.form Move up view when keyboard appear

                                Post-Redirect-Get with Spring WebFlux and Thymeleaf

                                Anylogic : not able to use stopDelay()