【发布时间】:2021-06-29 09:08:28
【问题描述】:
我正在尝试使用solidity 0.5.10、truffle 和web3 创建一个ETH 智能合约。一切似乎都很好,除了我得到:
ParserError:预期的编译指示、导入指令或合同/接口/库定义。 const web3 = require('web3');
当我尝试加载 web3 时。
我已经安装了 web3(dir {project folder} npm install web3)和我的 package.json(位于我的项目文件夹中):
“依赖”:{ “web3”:“^1.3.4” }
我都试过了: 从“web3”导入 Web3;
并且 const Web3 = require('web3');
但是还是无法加载web3,我做错了什么?
导致错误的合约
pragma solidity 0.5.10;
const web3 = require('web3');
contract UserRepository {
struct User {
uint id;
bytes32 firstName;
bytes32 lastName;
}
mapping(uint => User) public users;
uint public latestUserId = 0;
address private owner;
constructor() public {
owner = msg.sender;
}
}
package.json
{
"name": "helloworld",
"version": "1.0.0",
"main": "truffle-config.js",
"directories": {
"test": "test"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"web3": "^1.3.4"
}
}
【问题讨论】:
-
请编辑您的问题并显示导致错误的代码。 “预期的编译指示”解析错误看起来像
solc(solidity 编译器)错误,所以它可能与web3无关。 -
抱歉,现在包含@PetrHejda