Table of Contents
There is a module in node.js that allows the users to work with files and directories. This module is called the file module. It helps to create, delete, and edit files and directories along with many other functions. The node.js file system module abbreviated as FS can work with the file system and directories of your computer. In this article, we will discuss Node JS File System Module in Tamil.
Learn to code from industry experts! Get a free Demo here!
Node JS File System Module in Tamil (Full tutorial)
What is node.js?
Node.js is a tool that lets you run JavaScript code on your computer’s server. It is used to build websites and other internet based programs.
What is file system module in node.js?
The file system module in Node.js helps the user to interact with the file-system in the computer. It is a tool kit which helps you in creating, deleting and to perform other functions on the files and folders of the computer.
Node JS File System Module Tutorial
1: Which of the following is a JavaScript framework/library?
In any application in computer (eg: paint, word, excel etc) a file can be created by clicking on new file. An already existing file can be opened for reading by clicking on open. An opened file will be edited and updated by clicking on save file. All these functions are done with the help of an inbuilt module called FS in Node.js.
The common use of a File system Module are:
- Read Files
- Write Files
- Append Files
- Close Files
- Delete Files
To include the file system we make use of the require() method:
Var fs = require(fs);
To understand better let us learn how to create a file with an example:
Step 1: Open Visual Studio Code:
Launch VS Code on your computer.
Step 2: Create a New Folder or Open an Existing Project:
Navigate to the directory where you want to create the file, or open an existing project folder in VS Code.
Step 3: Open Terminal:
Click on the Terminal menu and select New Terminal to open a new terminal window within VS Code.
Step 4: Navigate to the Directory:
Use the cd command to navigate to the directory where you want to create the file.
For example:
bash code
cd path/to/your/directory
Step 5: Create a New JavaScript File:
Use the touch command to create a new JavaScript file. For example:
bash code
touch filename.js
Step 6: Open the File:
You can open the newly created file in VS Code either by clicking on it in the Explorer panel on the left-hand side or by using the command code filename.js in the terminal.
Step 7: Write Your JavaScript Code:
Inside the newly created JavaScript file, you can write your code. For example:
javascript code
// filename.js
const fs = require(‘fs’);
// Write to a file
fs.writeFileSync(‘output.txt’, ‘Hello, world!’);
Step 8: Save the File:
Once you’ve written your code, save the file by clicking on File > Save or using the shortcut Ctrl + S.
Step 9: Run Your JavaScript File:
You can now run your JavaScript file to create the output. If you’re using the require() function to include modules, make sure those modules are installed in your project’s node_modules directory. You can run the file using Node.js in the terminal:
code
node filename.js
Learn Coding in your Language! Enroll Here!
To understand the functions
-
appendFile()
-
openFile()
-
writeFile()
Using fs.appendFile():
- This method appends data to a file. If the file does not exist, it creates a new file.
Example:
Javascript code
const fs = require(‘fs’);
fs.appendFile(‘newfile.txt’, ‘Hello, world!’, function (err) {
if (err) throw err;
console.log(‘File created (or appended) successfully!’);
});
Using fs.open():
- This method opens a file. If the file does not exist, it creates a new file.
Example:
javascript code
const fs = require(‘fs’);
fs.open(‘newfile.txt’, ‘w’, function (err, file) {
if (err) throw err;
console.log(‘File created (or opened) successfully!’);
});
Using fs.writeFile():
- This method writes data to a file. If the file does not exist, it creates a new file.
Example:
javascript code
const fs = require(‘fs’);
fs.writeFile(‘newfile.txt’, ‘Hello, world!’, function (err) {
if (err) throw err;
console.log(‘File created (or overwritten) successfully!’);
});
In all three examples:
- ‘newfile.txt’ is the name of the file to be created.
- ‘Hello, world!’ is the data which is written to the file.
- The callback function is optional and is called when the file operation is completed.
- It typically takes an error parameter (err), which is null if the operation is successful, and a file parameter (file) for methods like fs.open().
Node JS File System Module in Tamil (Full tutorial): Video
Given below are the video links to the Node JS File System Module in Tamil (Full tutorial)
Topic | Video |
Node JS File System Module in Tamil – Part 1 | |
Node JS File System Module in Tamil – Part 2 |
Node JS File System Module in Tamil (Full tutorial): Conclusion
Node JS file system module is a toolbox which is used to perform various operation of the files and folders of the computer. The operations include to create, delete, append and more. A detailed tutorial of this file system was discussed in this blog. Also video links of the detailed tutorials of the node.js file system module in Tamil language was also shared.
Node JS File System Module in Tamil (Full tutorial): FAQs?
1. What is the Node.js File System (FS) module?
- It’s a toolbox in Node.js for working with files and folders on your computer.
- It helps you read, write, create, and delete files and folders.
2. What can I do with the FS module?
- You can read data from files, write data to files, create new folders, and delete files or folders.
3. How do I use the FS module in my Node.js applications?
- Include it in your code using const fs = require(‘fs’);.
- This lets you use its functions in your program.
Learn to code from industry experts! Get a free Demo here!