我使用 npm install 来安装我的节点包依赖项。我的节点包依赖项如下所示:
"dependencies": {
"blockcypher": "^0.2.0",
"fs-extra": "^7.0.1",
"ganache-cli": "^6.2.4",
"mocha": "^5.2.0",
"openzeppelin-solidity": "^2.0.0",
"solc": "^0.4.24"
}
当我使用 node 运行以下编译脚本时,我在具有引用node_modules依赖项的导入语句的每个合约中收到以下错误。
我正在使用固体版本:^0.4.24
Solidity Error: ParseError: Source "node_modules/openzeppelin-solidity/etc.."
const path = require("path"); //Delete all the contents in the build folder
const solc = require("solc"); //solidity compiler
const fs = require("fs-extra"); // Gives us access to the file system
//Require The Import Statements From The Contract
// import "node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
// import "node_modules/openzeppelin-solidity/contracts/ownership/Ownable.sol";
// import "node_modules/openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
//var data = require("node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.json");
//How do I use this?
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
const campainPath = path.resolve(__dirname, "Contract.sol");
// const campainPath = path.resolve(__dirname, "contracts", "Esgro.sol");
const source = fs.readFileSync(campainPath, "utf8");
console.log("nnt Compilation Output nnn");
console.log(solc.compile(source, 1));
const output = solc.compile(source, 1).contracts; //Internal Error
console.log("nnt Compilation Output nnn");
console.log(output);
fs.ensureDirSync(buildPath);
for (let contract in output) {
// fs.outputJsonSync(
// path.resolve(buildPath, contract.replace(":"", "") + ".json"),
// output[contract]
// );
console.log("nnt output[contract] nnn");
console.log(output[contract]);
fs.outputJsonSync(path.resolve(buildPath, contract), output[contract]);
}
//Compile both contracts with the solc compiler
//Write the output to the build directory
如何让编译器与本地计算机上的第三方依赖项"连接"?任何帮助或指示将不胜感激。
npm 依赖项导入 .sol 文件的正确语法如下:
import "openzeppelin-solidity/contracts/token/ERC20/ERC20Mintable.sol"; //You don't need to write node_modules
OpenZeppelin的文档说:
您需要一个以太坊开发框架来进行上述导入 声明工作!
因此,最好使用 Truffle 或 Embark 来编译和迁移您的合约。
最后一件事,OpenZeppelin使用版本^ 0.5.0的坚固性,因此,当您进行编译时,请记住使用合适的Solc版本。
让我知道它是否有效