Node.js File System Module helps you to work with files on your computer or server.You can read, edit, write, create or rename files.
To use file system you need to import them i.e var fs=require(‘fs’)
Reading Files
To read files on your computer we use function fs.readFile() function.
Let say you have below HTML file saved as “demo.html” in the same folder as your node.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<html> <body> </p> <h1>I love programming in Node.js</h1> <p> </p> <p>Node.js has in built modules like <br> </p> <ol> </p> <li>fs-for reading,writing,renaming files</li> <p> </p> <li>http-serving client request,act like a server</li> <p> </ol> <p> </p> <p> </body> </html> |
We can create a node.js code to read the file and display it in a web browser.
Save below code as file_demo.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
//Using Node.js File module to read a html File //File name demo.html var http=require('http'); var fs=require('fs'); http.createServer(function(req,res) { fs.readFile('demo.html',function(err,data) { res.writeHead(200,{'Content-type':'text/html'}); res.write(data); res.end(); }); }).listen(8080); |
Output:

Creating Files and adding content to them
Node.js File module has below functions for creating and adding content to files.
- fs.appendFile()-this function creates a file and adds content to it.If the specified file exists, the content is just added to it.Example code.
1 2 3 4 5 6 7 8 |
var fs=require('fs'); fs.appendFile('myfistfile.txt','Hello my first file',function(error) { if(error) throw error; console.log('File created and saved'); }) |
- fs.open()-this function takes a ‘flag’ w which means it opens the file for writing.If the file specified does not exists it created and it it exists its opened for writing.
Example
1 2 3 4 5 6 7 |
var fs=require('fs'); fs.open('myfistfile.txt','w',function(error,file) { if(error) throw error; console.log('File opened for writing'); }) |
- fs.writeFile()-this function creates a file and adds the content,if the file exists,its replaced with it content.
Example
1 2 3 4 5 6 |
var fs=require('fs'); fs.writeFile('myfistfile.txt','Hello my new content',function(error){ if(error) throw error; console.log('File content replaced'); }) |
Updating files
You can use fs.writeFile() and fs.appendFile() function to update files with new content.
How to delete files
fs.unlink() function is used to delete files.
Example code
If the file does not exist the function will throw an error.
1 2 3 4 5 6 |
var fs=require('fs'); fs.unlink('myfistfile.txt',function(error){ if(error) throw error; console.log('File deleted Successfully'); }) |
How to rename existing files
We can rename existing files using fs.rename() function
Example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
var fs=require('fs'); //Create the file you want to rename fs.appendFile('myfirstfile.txt','Hello my first content',function(error) { if(error) throw error console.log('File created ') }) //rename the file fs.rename('myfirstfile.txt','mynewfilename.txt',function(error) { if(error) throw error; console.log('File renamed successfully'); }) |