title: How to read/write JSON data between files? (Node.Js)
body: <p>I am working on a project which requires writing data to a JSON file from one module and calling it from another. However this does not happen, it returns <code>undefined:1 SyntaxError: Unexpected end of JSON input</code>.</p>
  
  <p>I need to use synchronous functions for this.</p>
  
  <p>Here is a sample file which shows my issue.</p>
  
  <p>Index.js</p>
  
  <pre><code>const fs = require('fs');
  const {idk} = require('./idk.js');
  
  fs.writeFileSync('output.json', '', 'utf8'); //Clears the file
  idk();// calls the module
  var result = fs.readFileSync('output.json', 'utf8'); //reads the file
  console.log(JSON.parse(result));//outputs a parsed file
  </code></pre>
  
  <p>Sample.js</p>
  
  <pre><code>const fs = require('fs');
  module.exports.sample = function(){
  
    var a = fs.readFileSync('output.json', 'utf8');//reads the file
    var b = JSON.parse(a);// parses json data
  
    console.log(b);// expected output {"instances": []}
  
    b.instances.push('yo'); //pushes to array
  
    var c = JSON.stringify(b);// stringify
  
    fs.writeFileSync('output.json', c, 'utf8'); //supposed to write '{"instances": ['yo']}'
  }
  </code></pre>