DEV Community

Mahima Bhardwaj
Mahima Bhardwaj

Posted on

1 1 1 1

FS Module in Node.js

File system module in Node.js provide an API for interacting with the file system on your computer. It allows you to perform various operations such as writing, creating , updating, deleting files and more.

To use fs.module ypu need to require it in your node.js application

Image description

Now,let’s explore some common use cases and example of working with file system module

1> Reading a File- You can read the content of using fs.read File method there’s Ex-
Node.js File system is responsible for all asynchronous or synchronous I/O operations.

Image description

fs.readFile is responsible for reading physical file asynchronously.

filename- full path and name of a file as string
options- parameter can be object or string which includes encoding is ‘utf-8’ and default flag is “r”.

callback Function -with 2 parameters err and fd. this will get called when read file operation completes.

*2>Writing a File
*

Image description
You can write data to a file using fs.writeFile method.

3>append a File- we can appendFile which takes three argument file name,data yo want to append and third callback function which will take parameter err . if there is any error it will throw err if there is no error then it will simply console log the msg.

Image description

Conclusion

What Kind of operation we can perform in node.js are as follows-
Importing the module

  1. Error Handling 2.Asynchronous and synchronous methods 3.File operations- reading, writing, updating, renaming, checking, deleting. 4.file streams

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay