Let’s see the below code example: const fs = require('fs')Ĭonst textRead = fs.readFileSync('./text/readfile.txt','utf-8') If you don’t use the character encoding then the output will be shown as a buffer. It takes two arguments, the first one is the path of the particular file that will be read and the next one is the character encoding. We already know about the fs module and this module helps us to read a file synchronously. In this section, we will read a file synchronously, and to do so we will use fs.readFileSync() function. We will import this module to work with the file system and read and write our file. It will require the module from NodeJS built-in module and is available for you to use it. Here, we have used const instead of using var and it is an ES6 feature. Let’s see the below code example of how you can import the fs module in your project. It will work both in a synchronous and asynchronous way. such as read file, write file, append file, and so on. The meaning of fs is a file system that helps you to work with files. NodeJS lets you perform different useful actions with its built-in modules. Nice to have little knowledge about NodeJS ‘fs’ if you don’t, not need to worry we will cover it later in this tutorial.But it is recommended not to use the node version under 10.0.0 while completing this tutorial. In this tutorial, we will use visual studio code. A text editor – You can choose any text editor on which you may write JavaScript code such as sublime text, nodepad++, etc.Basic knowledge of JavaScript, Node modules, etc.A configured computer with an internet connection.To complete this tutorial in the most effective way you need to keep some things in your mind. Before starting this tutorial, let’s see the prerequisite that will help you to get the best outcome. We will cover both of these in this tutorial and after completing this tutorial you will be able to read and write your own file from the computer both in a synchronous and an asynchronous way. You can read or write a file from your computer both synchronously or asynchronously. It is a very essential functionality that is provided by NodeJS and the implementation of these things is very easy. Reading and writing files is one of them. So, we can use fs.readFileSync() method for tasks like pausing other parallel processes and read a file synchronously.NodeJS is very fast and highly scalable because of its event-driven, single-threaded and, non-blocking I/O model and you can do many interesting things by using it. Observation: We can observe that when we are calling the fs.readFile() method which reads ‘input1.txt’ and after that we are calling the fs.readFileSync() method which reads the ‘input2.txt’ file, but seeing the output we find that ‘input2.txt’ file is readed first then ‘input1.txt’ file, this happens because when the compiler compiles the node.js file it first calls the fs.readFile() method which reads a file, but parallelly it continues to compile the remaining program too, after that it calls the fs.readFileSync() method which reads another file, when calling the readFileSync() method the compiler stops other parallel process (which is here reading the ‘input1.txt’ file from readFile() method) and continues the ongoing process to it’s end, whenever the ongoing process ends the compiler resumes the remaining process that’s why we observe that the contents of ‘input2.txt’ is printed first rather than ‘input1.txt’. (1) This is some text data which is stored in input1.txt file. Output: (2) This is some text data which is stored in input2.txt file.
How to update Node.js and NPM to next version ?.Creating objects in JavaScript (4 Different Ways).Introduction to Object Oriented Programming in JavaScript.Javascript | Error and Exceptional Handling With Examples.JavaScript | Importing and Exporting Modules.JavaScript | Program to write data in a text File.How to read a local text file using JavaScript?.ISRO CS Syllabus for Scientist/Engineer Exam.ISRO CS Original Papers and Official Keys.GATE CS Original Papers and Official Keys.