The fs (file system) module provides a set of methods to interact with the file system.
It allows us to perform various file-related operations, such as reading from and writing to files, creating directories, and more.
Here are file system operations using the "fs" module:
Reading the contents of a file asynchronously using "fs.readFile()".
const fs = require('fs'); fs.readFile('example.txt', 'utf8', (err, data) => { if (err) { console.error(`Error reading file: ${err.message}`); return; } console.log(`File Contents: ${data}`); });
Writing data to a file asynchronously using "fs.writeFile()".
const fs = require('fs'); const content = 'Hello World, Node.js!'; fs.writeFile('output.txt', content, 'utf8', (err) => { if (err) { console.error(`Error writing to file: ${err.message}`); return; } console.log('Data written to the file successfully.'); });
Reading the contents of a file synchronously using "fs.readFileSync()". readFileSync() method is a synchronous method. Node.js uses a single-threaded approach, this function will block the main thread until the function executes successfully. Node.js Server will not take any new request from the client until `fs.readFileSync` executes completely.
const fs = require('fs'); try { const data = fs.readFileSync('example.txt', 'utf8'); console.log(`File Contents: ${data}`); } catch (err) { console.error(`Error reading file: ${err.message}`); }
The Better approach is to use an asynchronous function in node.js, so it wouldn't block the main thread from taking new requests from client.
Writing data to a file synchronously using "fs.writeFileSync()". fs.writeFileSync() method is a synchronous method. Node.js uses a single-threaded approach, this function will block the main thread until the function executes successfully. Node.js Server will not take any new request from the client until `fs.writeFileSync` executes completely.
const fs = require('fs'); const content = 'Hello World, Node.js!'; try { fs.writeFileSync('output.txt', content, 'utf8'); console.log('Data written to file successfully.'); } catch (err) { console.error(`Error writing to file: ${err.message}`); }
The Better approach is to use an asynchronous function in node.js, so it wouldn't block the main thread from taking new requests from client.
Checking if a file or directory exists using "fs.existsSync()".
const fs = require('fs'); const filePath = 'example.txt'; if (fs.existsSync(filePath)) { console.log(`${filePath} exists.`); } else { console.log(`${filePath} does not exist.`); }
Creating a directory asynchronously using "fs.mkdir()".
const fs = require('fs'); fs.mkdir('new_directory', (err) => { if (err) { console.error(`Error creating directory: ${err.message}`); return; } console.log('Directory created successfully.'); });
Listing files in a directory asynchronously using "fs.readdir()".
const fs = require('fs'); const directoryPath = './'; fs.readdir(directoryPath, (err, files) => { if (err) { console.error(`Error reading directory: ${err.message}`); return; } console.log('Files in the directory:'); files.forEach((file) => { console.log(file); }); });