Solidity: ParserError: Expected pragma, import directive or contract /interface/library definition
Solution 1
This is not the case for you, but I'm going to leave this solution here for someone who may need this. I got this error when I forgot the semicolon ';' at the end of the first line pragma solidity ^0.4.25;
. So be sure to check that.
Solution 2
solc <= v0.4.25
Your primary issue using Solidity/solc v0.4.25 is your constructor definition.
You currently have your constructor defined as:
function Inbox(string passedName) public
However, defining constructors with the same name as the contract has been deprecated in Solidity. Try defining your constructor using the constructor
keyword instead.
constructor(string passedName) public
If you are using solc v0.4.25, please refer to the documentation in order to understand how to properly pass input to the compile
function. See my reference below:
const input = {
'Inbox.sol': fs.readFileSync(path.resolve(__dirname, 'contracts', 'Inbox.sol'), 'utf8')
}
const output= solc.compile({sources: input}, 1);
if(output.errors) {
output.errors.forEach(err => {
console.log(err);
});
} else {
const bytecode = output.contracts['Inbox.sol:Inbox'].bytecode;
const abi = output.contracts['Inbox.sol:Inbox'].interface;
console.log(`bytecode: ${bytecode}`);
console.log(`abi: ${JSON.stringify(JSON.parse(abi), null, 2)}`);
}
solc >= v0.5.0
If you are using Solidity/solc v0.5.2, you will also need to fix your constructor
definition. Furthermore, you will need to add the memory
keyword to each function
that returns or accepts the string
type.
For example:
function setMessage(string newMsg) public
should be declared as:
function setMessage(string memory newMsg) public
Futhermore, please see the latest documentation in order to understand the differences between the latest Solidity compiler and the older version. See my reference below for how to define the input for the compile
function utilizing the latest compiler:
const input = {
language: "Solidity",
sources: {
"Inbox.sol": {
content: fs.readFileSync(path.resolve(__dirname, "contracts", "Inbox.sol"), "utf8")
}
},
settings: {
outputSelection: {
"*": {
"*": [ "abi", "evm.bytecode" ]
}
}
}
}
const output = JSON.parse(solc.compile(JSON.stringify(input)));
if(output.errors) {
output.errors.forEach(err => {
console.log(err.formattedMessage);
});
} else {
const bytecode = output.contracts['Inbox.sol'].Inbox.evm.bytecode.object;
const abi = output.contracts['Inbox.sol'].Inbox.abi;
console.log(`bytecode: ${bytecode}`);
console.log(`abi: ${JSON.stringify(abi, null, 2)}`);
}
Solution 3
The problem is that you've not save your contract in UTF-8 encoding. to solve that you can open your contract file in a text editor and just save it as UTF8 (you can easily do it in VS code)
yogesh sharma
Updated on June 05, 2022Comments
-
yogesh sharma almost 2 years
I am getting error with both latest solc (0.5.2 version) and 0.4.25 too while I am writing Simple contract
I have tried following steps
- uninstalled Solc: npm uninstall solc
- Installed targeted version: npm install --save [email protected]
node compile.js (code given below)
{ contracts: {}, errors: [ ':1:1: ParserError: Expected pragma, import directive or contract /interface/library definition.\nD:\\RND\\BlockChain\\contracts\\Inbox.sol\n^\n' ],sourceList: [ '' ],sources: {} }
Compile.js
const path = require('path'); const fs = require('fs'); const solc = require('solc'); const inPath = path.resolve(__dirname,'contracts','Inbox.sol'); const src = fs.readFileSync(inPath,'UTF-8'); const res = solc.compile(inPath, 1); console.log(res);
Inbox.sol
pragma solidity ^0.4.25; contract Inbox { string message; function Inbox(string passedName) public { message = passedName; } function setMessage(string newMsg) public { message = newMsg; } function getMessage() public view returns(string){ return message; } }
Code worked well on Remix, for version 0.5.2 I have added memory tag to make it compile on Remix.
ex: function setMessage(string **memory** newMsg)
-
yogesh sharma over 5 yearsthanks Ben for your time, as mentioned in my comment I have tired with v 5 too. For brevity I have added v4 code above. I still get the same error.
-
yogesh sharma over 5 yearsthanks DAnsermino for your time. I have tried using as const res = solc.compile(JSON.stringify(inPath), 1); But it still giving me same error
-
Ben Beck over 5 years@yogeshsharma, please see my updated answer. I've included code samples for the older and latest versions of
solc
. -
yogesh sharma over 5 yearsThanks for your time and help. I will try this and let you know
-
DAnsermino over 5 yearsYou can't just pass in the path, you need to construct an import object. why are you including the
1
? If you read the docs you will see the second argument is an optional parameter for an import callback. -
Daniel Portugal about 2 yearsThis solved for me. It was saved with "UTF-8 with BOM" encoding. I changed it to UTF-8 and it worked.
-
who-aditya-nawandar almost 2 yearsHow do you change it in VSCode?