title: Create a json file from a html form
body: <br>
  I have a html form and I want to create a json-file with data introduced in html fields. <br>
  Right now, it is visible in console json-text but it doesn't create a new json-file with this content. <br> Also,I have an error, Uncaught ReferenceError: require is not defined. 
  <pre><code>    // get the form element from dom
      const formElement = document.querySelector('form#forms')
  
      // convert the form to JSON
      const getFormJSON = (form) =&gt; {
        const data = new FormData(form);
        return Array.from(data.keys()).reduce((result, key) =&gt; {
          if (result[key]) {
            result[key] = data.getAll(key)
            return result
          }
          result[key] = data.get(key);
          return result;
        }, {});
      };
  
      // handle the form submission event, prevent default form behaviour, check validity, convert form to JSON
      const handler = (event) =&gt; {
        event.preventDefault();
        const valid = formElement.reportValidity();
        if (valid) {
          const result = getFormJSON(formElement);
          // handle one, multiple or no files uploaded
          const images = [result.images].flat().filter((file) =&gt; !!file.name)
          // handle one, multiple or no languages selected
          const languages = [result.languages || []].flat();
          // convert the checkbox to a boolean
          const isHappyReader = !!(result.isHappyReader &amp;&amp; result.isHappyReader === 'on')
  
          // use spread function, but override the keys we've made changes to
          const output = {
            ...result,
            images,
            languages,
            isHappyReader
          }
          console.log(output)
        }
      }
  
      formElement.addEventListener(&quot;submit&quot;, handler)
  
  
  
      const fs = require('fs');
      const dataNew = JSON.stringify(output);
      fs.writeFile('output.json', dataNew, (err) =&gt; {
      if (err) {
          console.log(&quot;error&quot;)
          throw err;
      }
      console.log(&quot;JSON data is saved.&quot;);
  });
    &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>