Using internal Node.js modules
Node.js already has internal modules. They can also be used with the require(moduleName)
instruction seen previously.
Let’s look at an example of an internal module. There is, for example, the "fs"
module in the Node.js system. The name "fs"
is short for file system. This module allows you to interact with the internal file system of Node.js.
Now, we will use the "fs"
module to read the contents of a file.
Reading the contents of a file
Let’s use the "fs"
module to read the file named file1.txt
located in the current directory (where the test.js
file is located). Here are the contents of this file:
file1.txt file (in the directory where test.js is located)
This is the content of the file file1.txt located in the current directory.
The program that uses the "fs"
module and displays the contents of the file is as follows:
Reading and displaying the contents of the file...