我需要一些有关如何在初始化文件上加载功能文件并运行它的建议。我的实际功能文件充满了异步操作,我希望它可以在主初始化文件上运行,可能需要几行代码。函数文件的最终结果是将格式化的JSON文件保存到本地文件夹。
说我有myfuncfile.js
文件:
const fs = require(`fs`);
let myObj = {
"data1": "name1",
"data2": "name2"
}
fs.writeFile(`myjsonfile.json`, JSON.stringify(myObj), (err) => {
if (err) throw err;
console.log(`myjsonfile.json has been saved`);
});
我想在另一个文件myinitfile.js
上运行:
// Load myfuncfile.js into myinitfile.js
// const myfuncfile = fs.readFileSync('myfuncfile.js');
// Run that here
// myfuncfile();
谢谢您,祝您愉快。