ParserError:预期的编译指示,导入指令或合同/界面/库定义

时间:2019-09-11 01:14:11

标签: javascript compiler-errors solidity

每次将"ParserError: Expected pragma, import directive or contract/interface/library definition"compile.js命令一起使用来编译我的实体文件node compile.js时,都会出现错误Inbox.sol。我搜索了此错误,有两种解决方案,一种是由于我的语法错误,例如没有;,另一种是由于solidity编译器。我检查了我的版本和语法,找不到任何错误。请帮忙!

我已经尝试过this,但是它不是我的稳定版本,如果我将代码更改为contructor语法,它将弹出错误消息。

Inbox.sol

pragma solidity ^0.4.17;
contract Inbox{
  string public message;
  function Inbox(string initialMessage)public{
    message = initialMessage;
  }
  function setMessage(string newMessage) public{
    message = newMessage;
  }

}

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(JSON.stringify({
    language: 'Solidity',
    sources: {
      'Inbox.sol': {
        content: source,
      },
    },
    settings: {
      outputSelection: {
        '*': {
          '*': ['evm', 'bytecode'],
        },
      },
    },
  })));

package.json

{
  "name": "error",
  "version": "1.0.0",
  "description": "",
  "main": "compile.js",
  "scripts": {
    "test": "mocha"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "solc": "^0.4.17"
  }
}

package-lock.json

"solc": {
      "version": "0.4.17",
      "resolved": "https://registry.npmjs.org/solc/-/solc-0.4.17.tgz",
      "integrity": "sha512-39Tmo2r+qclwW7ooLXMLzMSxmoGtHy3/p2sDKdA9NM/+MRtzLm/AFKj4BY2Cocg3gwkfJzKTEx6X0wiI4fIZ/A==",
      "requires": {
        "fs-extra": "^0.30.0",
        "memorystream": "^0.3.1",
        "require-from-string": "^1.1.0",
        "semver": "^5.3.0",
        "yargs": "^4.7.1"
      }
  },

1 个答案:

答案 0 :(得分:0)

我将我的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));

然后就可以了!