Step 4

Notice the first statement in server.js file:

const fs = require("fs");

The "fs" is the File System Module, built into Node. Here, a module means a library. More broadly, a module is a programming construct that creates scope.

Every file in a Node application is considered a module.

Values defined are scoped in their file. If you want to use a value, you must explicitly export it from its file and then import it into another file.

The "fs" module exposes several functions, including the readdirSync and readdir that we just used. Previously, in working with React applications, we used ES6 modules. Nodes default module system is based on CommonJS.1

The ideas behind CommonJS are the same as those behind ES6 modules. The primary difference is the syntax of exports and imports.

For example, in using ES6, we would have imported the "fs" module as follows:

import fs from "fs";

However, with CommonJS, we use the following syntax:

const fs = require("fs");

We will cover CommonJS in more detail in the next chapter.

1

You can also use the ES6 module system with Node. Please refer to Node's document on ECMAScript 2015 (ES6) and beyond.