I want to create a folder (if folder does not exist) called alexadata and create a json file under this folder which contains 'data' and named with slashes like global/sites.json , else just want to create this json file into the alexadata folder. But both of two status, I have an error Uncaught Error: ENOENT: no such file or directory, open './global/sites.json' where if(err) return err; line. If folder does not exist, then it is created but json file is not created and gives this error. How can I fix this issue? Please help me. Thank you in advance.
jsToJSON: async (data) => {
try {
let url = await page.url();
const code = await page.evaluate(() => {
return {
code: document.querySelector('#alx-content > div.row-fluid.TopSites.AlexaTool.padding20 > section.page-product-top > div > section > div.Foldering > div.each.current > a').innerText.toLowerCase()
}
});
const fields = ['rank', 'name', 'url', 'last_update'];
const json2csvParser = new Json2csvParser({ fields });
var _path = path.join(__dirname,'alexadata');
if (!fs.existsSync("alexadata")) {
fs.mkdir("alexadata", (err) => {
if (err) return err;
fs.writeFileSync(`./${code.code}/sites.json`, JSON.stringify(data), 'utf-8', (err) => {
if (err) return err;
console.log("Directory and File Saved ");
});
});
} else {
fs.writeFile(path.join(_path, `/${code.code}/sites.json`), JSON.stringify(data), 'utf-8', (err) => {
if (err) return err;
console.log("Directory and File Saved ");
});
}
} catch (error) {
console.log(error)
}
}