I have the following JSON string created by javascript which is captured from a form in HTML.
I am trying to create a new JSON file using javascript but keep getting an error in the console.
here is the script:
let business_plans = [];
const add_business_plan = (ev) => {
ev.preventDefault(); //to stop the form submitting
let business_plan = {
id: Date.now(),
businessname: document.getElementById("businessName").value,
};
business_plans.push(business_plan);
document.querySelector("form").reset();
//for display purposes only
console.warn("added", { business_plans });
let pre = document.querySelector("#msg pre");
pre.textContent = "\n" + JSON.stringify(business_plans, "\t", 2);
var dictstring = JSON.stringify(business_plan);
var fs = require("fs");
fs.writeFile("thing.json", dictstring, function (err, result) {
if (err) console.log("error", err);
});
};
document.addEventListener("DOMContentLoaded", () => {
document
.getElementById("btn")
.addEventListener("click", add_business_plan);
});
here is the error in the console:
Uncaught ReferenceError: require is not defined
at HTMLButtonElement.add_business_plan (new_json.js:25)
the error is related to this line:
var fs = require("fs");
My question: What am I doing wrong and how do i fix this error?