Append an object to an existing JSON file

I’m relatively new to JS. I am working on a simple webpage which gets an input from user and manipulates it, and finally returns it as an object.

What I want is, to append my output objects to my existing .json file, each time user submits the form. I’ve googled it. It seems to have a solution using node.js… but when I try to import the “fs” module (I’ve my node.js installed), I got 2 errors:

1. require is not defined
2. import declarations may only appear at top level of a module

how should I fix this?

this is my code:

function getForm() {
    let activityType = document.getElementById("activity").value;
    let coefficient = 0;

    if (activityType === "semi-pushups") {
        coefficient = 2;
    } else if (activityType === "pushups") {
        coefficient = 4;
    } else if (activityType === "situps") {
        coefficient = 3;
    } else if (activityType === "squats") {
        coefficient = 5;
    } else if (activityType === "legraises") {
        coefficient = 7.5;
    } else if (activityType === "russian-twists") {
        coefficient = 3;
    } else if (activityType === "mixed") {
        coefficient = 10;
    } else if (activityType === "tubes") {
        coefficient = 0.25;
    }

    let dateData = document.getElementById("date").value;
    let repsData = Number(document.getElementById("reps").value);
    let xpData = repsData * coefficient;
    let myForm = {
        date: dateData,
        activity: activityType,
        reps: repsData,
        xp: xpData,
    };
    // console.log(myForm);
    console.log(myForm);
}
  • are you using any “require” statement
  • how are you importing “fs” module? show that code

happy coding :slight_smile: