I have "pragma solidity ^0.5.9" at the start of the file yet I still get this error.

Listing.sol:1:1: ParserError: Expected pragma, import directive or contract/interface/library definition.

pragma solidity ^0.5.9;

Here is the contract that I'm trying to convert into JSON:

pragma solidity ^0.5.9;

contract ListingGenerator {
    // initialize an array of deployed listing contracts
   Listing[] public deployedListings;

    // create a new listing and add it to array of deployed listing contracts
    function createListing(uint assessedValue, uint listPrice, string memory description, string memory location) public {
       Listing newListing = new Listing(assessedValue, listPrice, description, location, msg.sender);

    // return deployed listings
    function getDeployedListings() public view returns (Listing[] memory){
        return deployedListings;

contract Listing {
    uint public assessedValue;
    uint public listPrice;
    string public description;
    string public location;
    address public seller;
    bool public sold;
    mapping(address => bool) public buyers;
    uint public buyersCount;

    modifier restricted() {
        require(msg.sender == seller);

    constructor(uint propertyValue, uint salePrice, string memory propertyDetails, string memory propertyLocation, address owner) public {
        propertyValue = assessedValue;
        listPrice = salePrice;
        description = propertyDetails;
        location = propertyLocation;
        seller = owner;

    function confirmSale(uint confirm) public restricted returns(bool) {
        if (confirm == 1) {
            sold = true;
            return sold;

When I try to compile the contract into JSON format and save it into a build folder via:

// Import path, solc, and fs modules
const path = require('path');
const solc = require('solc');
const fs = require('fs-extra');

 *@return {ethereum/build}
function compilingPreparations() {
  const buildPath = path.resolve(__dirname, 'build'); // Get path to build folder
  fs.removeSync(buildPath); // Remove build folder and its content
  return buildPath; // Create new build folder

function createConfig() {
  return {
      language: 'Solidity',
      sources: {
        'Listing.sol': {
          content: fs.readFileSync(path.resolve(__dirname, 'contracts', 'Listing.sol'), 'utf8')

 * Compile the sources, defined in the config object with solc-js
 * @param config - Configuration object
 * @return {any} - Object with compiled sources and errors object
function compileSources(config) {
  try {
      return JSON.parse(solc.compile(JSON.stringify(config)));
    } catch (e) {}

 * Shows when there were errors during compilation
 * @param compiledSources
function errorHandling(compiledSources) {
  if (!compiledSources) {
    console.error('>>>>>>>>>>>>>>>ERRORS<<<<<<<<<<<<<<<\n', 'NO OUTPUT');
  } else if (compiledSources.errors) { // something went wrong
    compiledSources.errors.map(error => console.log(error.formattedMessage));

 * Writes the contracts from the compiled sources into JSON files, which you will later be able to
 * use in combination with web3.
 * @param compiled - Object containing the compiled contracts
 * @param buildPath - Path of the build folder
function writeOutput(compiled, buildPath) {

  for (let contractFileName in compiled.contracts) {
    const contractName = contractFileName.replace('.sol', '') + '.json';

const buildPath = compilingPreparations();
const config = createConfig();
const compiled = compileSources(config);
writeOutput(compiled, buildPath);

This is a refactored compile script(to fit my contract) that I found on a blog post on Medium.
It removes my app's build folder, then creates a new build folder but nothing is compiled into JSON format and then saved into the build folder due to whatever is throwing this error.

  • I just tested your code in Remix and it works fine... Did you try to compile it manually with 'solc --bin yourContract.sol' instead of refactored script to isolate the problem ? – m1k3y3 Jun 13 '19 at 18:15
  • I'd check if the input source is sent correctly to the compiler. Perhaps a BOM at the beginning might fool the compiler. – Ismael Jun 13 '19 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.