'ParserError while compiling solidity code

I am new to solidity. This is my 1st simple project when I do node compile.js & I am getting following error:

My Error :

{
contracts: {},
  errors: [
    ":7:16: ParserError: Expected identifier, got 'LParen'\n" +
      '    constructor(string initialMessage) public{\r\n' +   
      '               ^\n'
  ],
  sourceList: [ '' ],
  sources: {}
}

my compile.js

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

console.log(solc.compile(source,1));

my Inbox.sol

pragma solidity ^0.4.17;

contract Inbox {
    
    string public message;
    
    constructor(string initialMessage) public{
        message = initialMessage;
    }
    
    function setMessage(string newMessage) public{
        message = newMessage;
    }
}


Solution 1:[1]

I'm not sure why you are attempting to compile your smart contract natively using node. That is a fairly cumbersome approach (although it can be done, albeit with exponentially more work on your part), especially when tools already exist for this purpose.

The prefered method would be to use a tool such as the most recent build of truffle (which is a CLI tool available as a NodeJS package via NPM for compiling, deploying and testing smart contracts), instead of using NodeJS to compile.

Simply install truffle as a global package from a console using:

npm install -g truffle

Then navigate to your project root directory and run:

truffle init

This will generate a barebone project structure with everything that is required (such as directories for contracts and migrations).

After the project has been init'ed, open the truffle-config.js file and add the configurations required for your project:

module.exports = {
    networks: {
        //Configuration for localhost private dev network for testing code
        development: {
            host: "127.0.0.1",
            port: 8545,
            network_id: "*" //Match any network id
        }
        //Connecting to the main, live ethereum network requires different config
        //and should NOT be used for development purposes.
        //Only after your contracts have been thoroughly tested and audited should you deploy a production ready project to the main net.
    },
    compilers: {
        solc: {
            version: "0.6.2"
        }
    }
};

You can specify whichever compiler version suites your needs, however, most contracts now a days should target version 0.6.0 and up for the most recent features.

Learn more about customizing Truffle's configuration here!

Now, this allows you to specify both the network to deploy to and the specific solidity compiler (which truffle will automatically download the correct version if it does not already exist on your system) to run when compiling your project.

After this is complete, your contracts can be compiled easily using:

truffle compile --all --network development

Note that the argument for --network must match an entry in truffle-config.js's networks object (although just for compiling, it should not be necessary to specify a network, only for deployment).

Then there you go! You have compiled your first smart contract.

However, there is one caveat.

At this point you do not have a local test network to deploy to.

When deploying the contract, it's important to be aware to not deploy untested code to the live main network until it has been tested and audited.

Since any changes to the blockchain are permanent, deploying buggy contracts to the live blockchain can't be undone and any bugs can and will be exploited by malicious agents indefinitely.

To solve this issue, the trufflesuite team also provides ganache-cli (alongside various other helpful tools) for running a private test network on localhost. This package is also available via NPM however, that goes beyond the scope of this answer and your question.

Hope that helps you get started, best of luck!

Solution 2:[2]

The problem you are facing is a result of the compiler version. with solidity compiler version 0.4.17 all you need to do is replace the constructor keyword with the name of the contract i.e implicitly creating the constructor function. That should do the trick and yes your code editor will most likely complain especially if you have linting enabled but your codes will still run

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vigilance
Solution 2 Ehiz Eneboh