The fs Module in Node.js: A Short Guide to File System Interaction

RisingStack's services:

Node.js Experts

Learn more at risingstack.com

Sign up to our newsletter!

In this article:

Node.js is a powerful platform that lets you build fast, scalable network applications. One of the modules that comes with Node is fs, which provides access to the file system. In this article, we will give an overview of what the fs module does and how you can use it to interact with your files. We will also provide a tutorial on how to use some of its more common functions.

What does the fs module do?

The fs module provides a lot of functionality for interacting with the file system. Some of the more common functions that you will use are writeFile() / writeFileSync() and readFile() / readFileSync(). These functions let you write to and read from files, respectively.

So now that we briefly outlined what the fs module does, let’s take a look at how you can use it in your own applications. In our tutorial, we will show you how to write to and read from files, as well as get additional information about them.

How to use the fs module

We will start by creating a file called “file.txt”. This file will contain some text that we want to write to. Next, we will create a file called “readfile.js” and put the following code in it:

var fs = require('fs');
var file = 'file.txt' ;

fs.writeFile(file, 'Hello world!', function(err) {
  if(err) { 
    console . log ( err );
  } else { 
    console.log('The file was written successfully!');
  }
});

var contents = fs.readFileSync(file);

console.log(contents);

We first require the fs module. Then we create a variable, which contains the path to our “file.txt” file. Next, we use the writeFile() function to write the text “Hello world!” to disk. We pass it three parameters: the file to write to, the text to write, and a function that will be executed if there are any errors.

The Node.js fs module provides two different functions for writing files: writeFile and writeFileSync. Both functions take a file path and data as arguments, and write the data to the specified file. However, there is a key difference between the two functions: writeFile is asynchronous, while writeFileSync is synchronous. This means that writeFile will return immediately, before the file has been written and only its callback will be called when the write operation is completed, while writeFileSync will block until the file has been written. As a result, writeFile allows your script to handle other tasks, while the computer is busy writing the file, but writeFileSync can be easier to use if you need to be sure that the file has been especially when bootstrapping your process. Most fs functions have a sync and an async version just like readFile and writeFile.

If everything goes well, the function will execute and print “The file was written successfully!” to the console. If there are any errors, it will print them out.

Next, we use the readFileSync() function to read the contents of our “file.txt” file into a variable called contents. We then log the contents of the variable to the console.

And that’s all there is to it! You can now use these same concepts to do more complex tasks with files, such as reading from multiple files at once or writing formatted data. Be sure to check out the fs module documentation for more information.

Happy coding! 🙂

Share this post

Twitter
Facebook
LinkedIn
Reddit