The `fs` (File System) module is a built-in Node.js module that provides functions for working with the file system. Here's an example of using the `fs` module to read the contents of a file and print them to the console:
This code reads the contents of the file `'file.txt'` and prints them to the console. The `'utf8'` parameter specifies that the file should be interpreted as UTF-8 encoded text. If you want to read the file as a binary buffer, you can omit this parameter or use `null` instead.
Here's an example of writing to a file:
This code overwrites the contents of the file 'file.txt' with the string 'Hello, world!'. If the file does not exist, it will be created.
The fs module provides many other functions for working with the file system, such as reading and writing files asynchronously and synchronously, creating and deleting directories, and renaming files. For more information and a complete list of functions, you can refer to the Node.js documentation.

Posting Komentar