title: How to add an element to a JSON array file from a form using JS
body: <p>I have this html form to add data to a json array file</p>
  
  <pre><code>&lt;head&gt;
          &lt;script type="text/javascript" src="require.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript" src="AddBook.js"&gt;&lt;/script&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;form id="add"&gt;
              &lt;input type="text" id="isbn" name="isbn"&gt;
              &lt;br&gt;
              &lt;input type="text" id="title" name="title"&gt;
              &lt;br&gt;
              &lt;input type="text" id="author" name="author"&gt;
              &lt;br&gt;
              &lt;input type="text" id="issued" name="issued"&gt;
              &lt;br&gt;
              &lt;input type="submit" name="submit"&gt;
          &lt;/form&gt;
          &lt;script&gt;
                  var x = document.getElementById("add");
                  var isbn = x.elements[0].value;
                  var title = x.elements[1].value;
                  var author = x.elements[2].value;
                  var issued = x.elements[3].value;
                  write(isbn,title,author,issued);
          &lt;/script&gt;
  </code></pre>
  
  <p>and below is the AddBook.js</p>
  
  <pre><code>function write(isbn, title, author, issued) {
    const fs = require(['fs'], function (fs){  
    const data = fs.readFileSync('js/book.json');
    let jsonStr = data.toString();
    const obj = JSON.parse(jsonStr);
  
    obj.books.push({
      isbn,
      title,
      author,
      issued,
    });
    jsonStr = JSON.stringify(obj);
    console.log(jsonStr);
    fs.truncate('js/book.json', 0, () =&gt; {
      fs.writeFile('js/book.json', jsonStr, (err) =&gt; {
        if (err) {
          throw new Error(`Error writing file: ${err}`);
        }
      });
    });
    });
  }
  </code></pre>
  
  <p>The problem is that the below script works as a standalone file but stops working as soon as it is used with the HTML. 
  How can I make this script work with the form</p>