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.
- Unlink a file
const fs = require('fs');
const callbackFun = (err) => {};
fs.unlink('/tmp/me.text', callbackFun);
- Write a file
const fs = require('fs');
const data = new Uint8Array(Buffer.from('Onkar'));
const callbackFun = (err) => {};
fs.writeFile('/tmp/me.text', data, callbackFun);
- 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.
- Unlink a file
const fs = require('fs');
fs.unlinkSync('/tmp/me.png');
- Write a file
const fs = require('fs');
const data = new Uint8Array(Buffer.from('Onkar'));
fs.writeFileSync('/tmp/me.text', data);
- Read a file
const fs = require('fs');
const data = fs.readFileSync('/tmp/me.text');