NodeJS - Using fs module to read write to files


About

fs is provided by simple wrappers around standard POSIX functions. To use this module do require(‘fs’). All the methods have asynchronous and synchronous forms.

Deleting file

ASync Version

const fs = require('fs');

fs.unlink('/tmp/hello', (err) => {
  if (err) throw err;
  console.log('successfully deleted /tmp/hello');
});

Sync Version

const fs = require('fs');

fs.unlinkSync('/tmp/hello');
console.log('successfully deleted /tmp/hello');

Reading file

Sync Version

var notesString = fs.readFileSync('notes-data.json');
var notesFromFile = JSON.parse(notesString);
Writing to file

Sync Version

// notes loaded from memory
var myNotesInMemory = JSON.parse(fs.readFileSync('notes-data.json')); 
// notes being written to file
fs.writeFileSync('notes-data.json', JSON.stringify(notes));