support@codebucket.net

Learn about NodeJs FS (File System) Module

Learn about NodeJs FS (File System) Module

K. M. Shawkat Zamil | September 26, 2022

Hello developers,

In this demonstration, I will discuss NodeJs FS Module. FS means File System Module. NodeJs includes the fs module to access the physical file system. The fs module is responsible for all the asynchronous or synchronous file i/O operations.

 

Synchronous ⇒ Wait to finish, After finish, you can move

Asynchronous ⇒ No need to wait, It will run in the background.

 

Necessary methods for Node.Js fs module essential operations Asynchronous:

 

Method Description
fs.readFile(fileName, [options], callback) Reads existing file
fs.writeFile(fileName, data[options], callback) Writes to the file. If the file exists then overwrite the content otherwise creates a new file
fs.rename(oldPath, newPath, Callback) Renames an existing file
fs.exists(path, callback) Determines whether the specified file exists or not
fs.unlink(path, callback) File delete
fs.append(path, data[options], callback) Appends new contents to the existing file
fs.open(path, flags[mode], callback) Opens file for reading or writing
fs.mkdir(path, [mode], callback) Creates a new directory
fs.rmdir(path, callback) Renames an existing directory
fs.readdir(path, callback) Reads the content of the specified directory

 

Necessary methods for Node.Js fs module essential operations Synchronous:

 

Method Description
fs.readFileSync(fileName, [options]) Reads existing file
fs.writeFileSync(fileName, data[options]) Writes to the file. If the file exists then overwrite the content otherwise creates a new file
fs.renameSync(oldPath, newPath) Renames an existing file
fs.existsSync(path) Determines whether the specified file exists or not
fs.unlinkSync(path) File delete
fs.appendSync(path, data[options]) Appends new contents to the existing file
fs.openSync(path, flags[mode]) Opens file for reading or writing
fs.mkdirSync(path, [mode]) Creates a new directory
fs.rmdirSync(path) Renames an existing directory
fs.readdirSync(path) Reads the content of the specified directory

 

Hope this might help in the journey of development.

 

Read More: Working with URL module in NodeJs

 

K. M. Shawkat Zamil

K. M. Shawkat Zamil

Senior Software Engineer

I am a Senior Software Engineer in a reputed company in Bangladesh. I am a big fan of Laravel, PHP Programming, MSSQL Server, MySql, JavaScript, and lots more. I love to help people who are especially eager to learn. I believe in patience and motivation.