The fs Module

Overview

The fs module is a built-in global Node.js module that exposes an API for accessing, manipulating, and interacting with the file system.

We’ll use a few methods from the fs module to read and write information to test.txt.

All fs operations are available either in synchronous or asynchronous forms (click here for details).

In this chapter, we’ll use a newer feature of fs, called the fs Promises API, which returns Promise objects so that we can use the async and await keywords instead of callbacks to handle asynchronicity.

We can access the API by importing it at the top of the file via require('fs').promises.

Load the fs module

The module system uses the require() function to load in a module and access its contents.

To load in the fs module and get access to its properties and methods, we use require() at the top of the file-system.js file:

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy