Node Modules
Hello Everyone! this will be my 3rd blog of my Backend Development series.
In this blog, I will be sharing my understanding of node modules and also I will showcase how to use a module named file system (fs).
What are modules ?
Modules In Node Js is like body parts of a human body, everything we do moving, walking, eating, etc involves our body parts. Just like that modules are body parts of NodeJS and everything in it is a module.
For example, the server which I created in last blog was using a module http.
According to the docs modules are libraries which can be integrated In Node.js, modules are a fundamental concept used to organize and encapsulate code. They allow you to break your code into smaller, reusable pieces, making it easier to manage, maintain, and share your codebase. Modules also help prevent naming conflicts and allow you to keep your code clean and organized.
Node.js uses a module system called the CommonJS module system, which provides a way to define, import, and export modules.
Let's understand this better
The best way to discuss these modules is by implementing them. In the last blog, I created a server where I have already shown how to import a module. You can check that blog here .
In this blog, I will use the FS module to read and write files
But first What is the fs module?
file system module in Node provides ways to read a file use its data and create a file within an existing project.
Let's look at the setup first,
Create a folder and initialize it with npm
init
The image below shows the folder structure and files.
This is a sample program to showcase case use of the fs module, I highly suggest going for official docs to know more about this module.
Here, there is a src folder that contains files that are supposed to be used in the index.js program.
Then in index.js, import the module fs by
const fs = require('fs');
After importing here is how we can read the file by use
fs.readFile
method
fs.readFile('./src/1st.txt','utf-8',(err,data)=>{
console.log(data);
});
Here, there are three arguments,
Is the path to the file that we want to read
specifies the language
a call-back function that will take two arguments err(error) and data.
This data variable is directly can be console logged to print data which is in 1st.txt file.
Let's now write something in another file.
For that, there is a function same as readFile i.e., `fs.writeFile`
First, let's just create a variable of dummy text
const words = 'We are rocking'
Now,
fs.writeFile("./src/2nd.txt", `${words}`, "utf-8", (err) => { console.log("Your file has been written"); });
Arguments are the same as the readFile method but there is just another argument where the variable we created is passed in template strings.
Now if we check the file
2nd.txt
,
Conclusion
This is how fs module is used.
Note: The demonstration I have shown here is very introductory level please refer the official docs .
Subscribe to my newsletter
Read articles from Parth Tiwari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Parth Tiwari
Parth Tiwari
I am a amateur web developer currently learningExpress and eventually going to be a DevOps engineer.