1

I am practicing writing smart contracts in Solidity and running the code below results in no such file or directory error.

Error: ENOENT: no such file or directory, open 'C:\kickstart\ethereum\build:Campaign.json'

This is a Kickstarter type contract with multiple participants and a manager(owner of the contract). Users are able to donate funds. The manager is the only one who is able to withdraw money, but other user`s permission is needed for that type of action.

 **Campaign.sol**
    pragma solidity ^0.4.20;

contract CampaignFactory {
    address[] public deployedCampaigns;


    function createCampaign(uint minimum) public {
     address newCampaign = new Campaign(minimum, msg.sender);
     deployedCampaigns.push(newCampaign);
    }

    function getDeployedCampaigns() public view returns(address []) {
        return deployedCampaigns;
    }

}
contract Campaign {
    struct Request {
        string description;
        uint value;
        address recipient;
        bool complete;
        uint approvalCount;
        mapping(address => bool) approvals;
            }

Request[] public requests;
address public manager;

uint public minimumContribution;
//address[] public approvers; mapping has a constant time lookup
mapping(address => bool) public approvers;
uint public approversCount;

modifier restricted() {
    require(msg.sender == manager);
    _;
}

constructor (uint minimum, address creator) public {
    manager = creator;
    minimumContribution = minimum;
}

function contribute() public payable {
    require(msg.value > minimumContribution);

  approvers[msg.sender] = true;  
  approversCount++;

}

function createRequest(string description, uint value, address recipient) public restricted {
    Request memory newRequest = Request({
        description: description,
        value:value,
        recipient: recipient,
        complete: false,
        approvalCount:0
    });

    requests.push(newRequest);
     }

     function approveRequest(uint index) public {
         Request storage request = requests[index];

         require (approvers [msg.sender]);
         require(!request.approvals[msg.sender]);

         request.approvals[msg.sender] = true;
         request.approvalCount++;



     }

     function finalizeRequest(uint index) public restricted {
     Request storage request = requests[index];

     require(!request.complete);
     request.complete = true;

     }
 }

**compile.js**
const path = require("path");
const solc = require("solc");
//file system on local compuiter
const fs = require("fs-extra");

const buildPath = path.resolve(__dirname, "build");
// file removal in fs-extra version
fs.removeSync(buildPath);
//path to the file
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
//get a content of file
const source = fs.readFileSync(campaignPath, "utf8");
// output from the compiler
const output = solc.compile(source, 1).contracts;

for (let contract in output) {
  fs.outputJsonSync(
    path.resolve(buildPath, contract + ".json"),
    output[contract]
  );
}
4

4 回答 4

3

回答您的问题:Campain.json 位于 build 文件夹中,包含所有 Campaign 合同实例的输出。

我能够通过执行以下步骤来修复错误:

  • 彻底删除项目中的node_modules文件夹
  • 运行 npm cache clean --force 命令
  • 运行 npm 安装

请在下面找到该项目的链接: https ://github.com/StephenGrider/EthereumCasts

于 2018-10-04T03:41:22.243 回答
1

我遇到了同样的问题。问题是 Windows 不喜欢在文件名中包含特殊字符,在这种情况下是冒号“:”字符。

因此,在输出结果时删除冒号,如下所示:

for (let contract in output) {
  fs.outputJSONSync(
    path.resolve(buildPath, contract.replace(':', '') + ".json"),
    output[contract]
  );
}
于 2019-04-15T07:39:23.290 回答
0

如果您更改了文件名,则需要迁移它。

npx truffle migrate --reset --compile-all

我有同样的问题,这对我有用。

于 2021-08-17T09:15:43.150 回答
0

如果您想确保您的 compile.js 文件配置正确,请在 for 循环之前添加“ console.log(solc.compile(source, 1)); ”并检查输出。

关于这个问题,您可以替换: path.resolve(buildPath, contract + '.json'),用 path.resolve(buildPath, contract.replace(':', '') + '.json')

希望这可以帮助 !

于 2019-03-05T07:27:34.823 回答