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 .
solc.compile(JSON.stringify(input))
is not valid JSON would be my assumption without running the code.