Node.js fs module
1 min read

Node.js fs module

What this article includes:

  • What is node fs module?
  • What are the asynchronous functions?
  • What are the synchronous functions?
  • References to learn more

What is node fs module?

fs stands for File System. File system module is used to write and read a file. fs is part of node core modules. fs module provides both synchronous and asynchronous functions for the behaviors like read file, write file, delete file.

What are the asynchronous functions?

Ususally fs asynchronous functions always requires a callback function and it does not block further execution.

  1. Unlink a file
const fs = require('fs');
const callbackFun = (err) => {};
fs.unlink('/tmp/me.text', callbackFun);
  1. Write a file
const fs = require('fs');
const data = new Uint8Array(Buffer.from('Onkar'));
const callbackFun = (err) => {};
fs.writeFile('/tmp/me.text', data, callbackFun);
  1. Read a file
const fs = require('fs');
const callbackFun = (err, data) => {};
fs.readFile('/tmp/me.text', callbackFun);

What are the synchronous functions?

Synchronous function will block further execution till the operation is completed and returns the output for the function.

  1. Unlink a file
const fs = require('fs');
fs.unlinkSync('/tmp/me.png');
  1. Write a file
const fs = require('fs');
const data = new Uint8Array(Buffer.from('Onkar'));
fs.writeFileSync('/tmp/me.text', data);
  1. Read a file
const fs = require('fs');
const data = fs.readFileSync('/tmp/me.text');

References to learn more