我该如何修复来自 fs.outputJsonSync node.js 的错误
How can i fix the error coming from fs.outputJsonSync node.js
我正在使用 node js 编译智能合约。
我想从这个智能合约中导入两个对象(代表两个合约)并将它们存储在名为“build”的文件目录中,扩展名为 JSON。
当我 运行 命令节点 compile.js 时,我得到了这个错误:
errno: -4058, syscall: 'open', code: 'ENOENT'.
当我调试我的代码时,错误发生在 fs.outputJsonSync?
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
const source = fs.readFileSync(campaignPath, "utf8");
const output = solc.compile(source, 1).contracts;
console.log(output);
fs.ensureDirSync(buildPath);
// To loop throught the contracts that contains 2 objects with data
for (let contract in output) {
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
output[contract]
);
}
此错误表明文件不存在。读取文件时出现错误,请确保在读取文件之前存在该文件,因为 fs.outputJsonSync
如果文件不存在则创建该文件。
// Function call
// Using callback function
fs.outputJSON(file, {name: "David"}, err => {
if(err) return console.log(err);
console.log("Object written to given JSON file");
});
尽量保留try/catch中的readFileSync
方法:
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
let output;
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
try {
const source = fs.readFileSync(campaignPath, "utf8");
output = solc.compile(source, 1).contracts;
console.log(output);
fs.ensureDirSync(buildPath);
} catch (e) {
console.log(`Error while reading the file ${e}`);
}
// To loop throught the contracts that contains 2 objects with data
for (let contract in output) {
try {
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
output[contract]
);
} catch (e) {
console.log(`Error while writing JSON: ${e}`);
}
}
最好把不同的操作包在不同的try/catch下,以便错误分离。
我正在使用 node js 编译智能合约。
我想从这个智能合约中导入两个对象(代表两个合约)并将它们存储在名为“build”的文件目录中,扩展名为 JSON。
当我 运行 命令节点 compile.js 时,我得到了这个错误:
errno: -4058, syscall: 'open', code: 'ENOENT'.
当我调试我的代码时,错误发生在 fs.outputJsonSync?
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
const source = fs.readFileSync(campaignPath, "utf8");
const output = solc.compile(source, 1).contracts;
console.log(output);
fs.ensureDirSync(buildPath);
// To loop throught the contracts that contains 2 objects with data
for (let contract in output) {
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
output[contract]
);
}
此错误表明文件不存在。读取文件时出现错误,请确保在读取文件之前存在该文件,因为 fs.outputJsonSync
如果文件不存在则创建该文件。
// Function call
// Using callback function
fs.outputJSON(file, {name: "David"}, err => {
if(err) return console.log(err);
console.log("Object written to given JSON file");
});
尽量保留try/catch中的readFileSync
方法:
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
let output;
const campaignPath = path.resolve(__dirname, "contracts", "Campaign.sol");
try {
const source = fs.readFileSync(campaignPath, "utf8");
output = solc.compile(source, 1).contracts;
console.log(output);
fs.ensureDirSync(buildPath);
} catch (e) {
console.log(`Error while reading the file ${e}`);
}
// To loop throught the contracts that contains 2 objects with data
for (let contract in output) {
try {
fs.outputJsonSync(
path.resolve(buildPath, contract + ".json"),
output[contract]
);
} catch (e) {
console.log(`Error while writing JSON: ${e}`);
}
}
最好把不同的操作包在不同的try/catch下,以便错误分离。