The fs
module is a core module provided by Node.js itself, not available by default in JavaScript. This module plays a crucial role in interacting with the file system, allowing developers to perform various file-related operations such as reading, writing, appending, deleting, and more.
Writing to a File
fs.writeFileSync("./created.txt", "Created file using writeFileSync");
This line uses the synchronous version of the
writeFile
function calledwriteFileSync
, to write data to a file named "created.txt".The first parameter is the file path, which in this case is "./created.txt" (relative path).
The second parameter is the data you want to write to the file, which is the string "Created file using writeFileSync".
fs.writeFile("./created.txt", "Created file using writeFile", (err) => { ... });
This line uses the asynchronous version of the
writeFile
function, calledwriteFile
, to write data to a file named "created.txt".Similarly, the first parameter is the file path.
The second parameter is the data to be written.
The third parameter is a callback function that is invoked after the write operation is completed or if an error occurs
Thus for every method provide we have either Async or Sync way to do the operation, when it is synchronous then it doesn't require a callback function. I wrote another blog that specifies Blocking vs Non-Blocking execution which is very important from an interview perspective and application-wise.
Reading from a File
const result = fs.readFileSync("./created.txt", "utf-8");
These lines read the contents of the file "created.txt" synchronously using the
readFileSync
function.The first parameter is the file path.
The second parameter is the encoding, which is set to "utf-8" to read the file as a text file.
The result is stored in the
result
variable, and it is logged to the console.
Some alternative encodings include ASCII (for basic English characters), UTF-16 (uses 2 bytes per character), and ISO-8859-1 (also known as Latin-1).
In many cases, UTF-8 is a good choice because of its compatibility and support for a wide range of characters.
fs.readFile("./created.txt", "utf-8", (err, result) => { ... });
These lines read the contents of the file "created.txt" asynchronously using the
readFile
function.The first two parameters are the file path and the encoding, similar to the
readFileSync
function.The third parameter is a callback function that is invoked with two arguments: an error (if any) and the file contents.
Appending to a File
fs.appendFileSync("./log.txt", "Data logged appended \n");
This line demonstrates how to append data to a file using the
appendFileSync
function.The first parameter is the file path.
The second parameter is the data to be appended to the file.
One common use case of
appendFileSync
is creating and maintaining a log file that stores a chronological record of events and logs. This log file can be useful for debugging, error tracking, or general auditing purposes. Here's an example scenario to illustrate this use case:
Copying a File
fs.copyFileSync("./created.txt", "./copy.txt");
This line copies the contents of "created.txt" to "copy.txt" using the
copyFileSync
function.The first parameter is the source file path, and the second parameter is the destination file path.
Conclusion
In this article, we explored the basics of file streams in Node.js using the fs
module. We covered how to write to a file, read from a file, append data to a file, and copy a file. By utilizing these file stream operations, you can efficiently handle file I/O tasks in your Node.js applications.
Please note that when working with file streams, it's important to handle errors appropriately and close the streams after use to prevent resource leaks. Additionally, the asynchronous functions are generally preferred in Node.js applications to ensure non-blocking behavior.
Thanks a lot for reading the article.
Hope you found it helpful.
Linkedin: linkedin.com/in/tautikk
Email: tautikagrahari@gmail.com
Twitter: twitter.com/TautikA