0

I am learning solidity following a course. I am completely noob at solidity . I am trying to run my compile.js file using command "node compile.js" .But I am getting same error saying

"SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse ()"

I have tried mulitiple solution to fix this . I did match my solidity version with compiler version . After trying multiple solution ,it seems not to work . Dont know where I am missing the spot .

Here is my compile.js file :

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

const input = {
    language: 'Solidity',
    sources: {
        'Inbox.sol': {
            content: source,
        },
    },
    settings: {
        outputSelection: {
            '*': {
                '*': ['*'],
            },
        },
    },
};

module.exports = JSON.parse(solc.compile(JSON.stringify(input))).contracts['Inbox.sol'].Inbox;

My main contract code :

pragma solidity >=0.4.25 ;

contract inboxx {
    string public message;

    constructor(string memory intialmsg) {
        message = intialmsg;
    }

    function setmessage(string memory newMessage) public {
        message = newMessage;
    }
}

My package.json

{
  "name": "inbox",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "ganache-cli": "^6.12.2",
    "mocha": "^10.2.0",
    "openzeppelin-solidity": "^4.6.0",
    "solc": "^0.4.25",
    "web3": "^1.0.0-beta.26"
  }
}

Dont want to quit here . Thanks a lot .

1
  • solc.compile(JSON.stringify(input)) is not valid JSON would be my assumption without running the code. Jan 6, 2023 at 16:13

2 Answers 2

0

It's related to the version of your solc.

try that instead:

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

const input = {
    'Inbox.sol': source
};
const str = JSON.stringify(input);
const compiled = solc.compile({ sources: input }, 1);
module.exports = {
    abi: compiled.contracts['Inbox.sol:Inbox'].bytecode,
    bytecode: compiled.contracts['Inbox.sol:Inbox'].interface,
}

notice the change in the constructor adding the public keyword

pragma solidity >=0.4.25;

contract Inbox {
    string public message;

    constructor(string memory intialmsg) public{
        message = intialmsg;
    }

    function setmessage(string memory newMessage) public {
        message = newMessage;
    }
}

Check this similar question on SO https://stackoverflow.com/a/53950041/2124936

7
  • Hi thanks for reaching , I have change my code to yours ,now its showing error saying "Maximum call stack size exceeded" in VS terminal
    – Shorna
    Jan 6, 2023 at 17:06
  • I ran the code here and it is running normally. It is hard to tell what is going on without the stack trace of the error Jan 6, 2023 at 17:08
  • My VS terminal showing this error : RangeError: Maximum call stack size exceeded at Object.$db [as dynCall_viiiiii] (G:\Course\inbox\inbox\node_modules\solc\soljson.js:12:120931) at invoke_viiiiii (G:\Course\inbox\inbox\node_modules\solc\soljson.js:1:1118207) at Array.pva (G:\Course\inbox\inbox\node_modules\solc\soljson.js:13:29030) at Object.M9a [as dynCall_vi] (G:\Course\inbox\inbox\node_modules\solc\soljson.js:12:99033) at invoke_vi (G:\Course\inbox\inbox\node_modules\solc\soljson.js:1:1115011) my vs terminal showing this error
    – Shorna
    Jan 6, 2023 at 17:29
  • try to run on node version v16.17.0 Jan 6, 2023 at 17:47
  • It seems to be an issue with solc js (github.com/ethereum/solc-js/issues/537) another possible way would be to run with the command: node --stack-size=2000 compile.js Jan 6, 2023 at 17:49
0

you should do this instead.

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, 'UTF-8');

var input = {
    language: 'Solidity',
    sources: {
        [inboxPath] : {
            content: source
        }
    },
    settings: {
        outputSelection: {
            '*': {
                '*': [ '*' ]
            }
        }
    }
}; 
var output = JSON.parse(solc.compile(JSON.stringify(input)));

module.exports = {
  abi: output.contracts[[inboxPath]]["Inbox"].abi,
  bytecode: output.contracts[[inboxPath]]["Inbox"].evm.bytecode.object,
};

Hope this helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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