The File System in Node.js

Image for post
Image for post

In my last article, I introduced Node.js and went over what it was and what it meant for the Javascript programming language. Until Node, Javascript was confined to the browser and used primarily to create dynamic web content. Node allows Javascript to be used as a general-purpose programming language which means that it can be used across various application domains. We will take a look at one of the many ways Node extends Javascript, by providing it access to the file system.

Before we go any further, we need to install Node. To do this, you can visit the Node.js website, listed below, and follow the installer instructions. The installation is easy and facilitated by an installation wizard.

After installing Node, you begin using it by simply entering “node” in the terminal. By doing so, you open the Node REPL where you can begin writing code snippets. In addition, you can write entire scripts using your text editor and execute them like so:

node filename.js

Before we start working with the file system, I would like to point out that Node provides this functionality in the same manner that the browser provides the functionality of manipulating the DOM. The file system is a core module in Node and by importing it, you gain access to various encapsulated methods. Also, as mention previously, since Node uses a non-blocking I/O model, it provides two types of methods for each process, synchronous and asynchronous. We can begin by using a very important function within the Node environment for importing modules, the “require()” function. It works in the way as the ES6 equivalence for working with modules, “import”, “export” and “import()”.

const fs = require('fs');

With this line, we import the module and store it within the variable of the same name, which is suggested by Node. Next, we can begin utilizing some of the methods afforded by the module. The first method we can look at is “writeFileSync” which allows you to write to a file, synchronously, overwriting the contents of the file if the file exists, and creating the file with the content added if the file does not exist.

// index.js

At this point, if the “notes.txt” would be created within the directory of the script file “index.js” with the added string. From here we can also add to the file without overwriting the text we just added like so:

// index.js

I also want to point out that technically each time the “index.js” file is executed, especially in the case of wanting to append to the “notes.txt” file, the “notes.txt” file is overwritten by the “writeFileSync” method.

In addition to strings being written to files, other Javascript data structures can be written, they only need to be in string format. For example, you can write an object to a file like so:

// index.js

Using the JSON “parse” method, the object is converted to a string which allows it to be written to the file “app.js”. In order to read what is stored in a file requires a bit more work but is still simple enough. That process goes as follows:

// index.js

Again, reading requires a few more steps. First, what is returned from executing the “readFileSync” method is a buffer. A buffer is a region of memory that is used when data is being moved from one place to another. In this case, the process of reading or retrieving data from one file to another causes the return of a buffer. After the buffer is returned, it is converted to a string using the “toString” String method. An alternative approach for handling the data buffer could be using another Node core module, the StringDecoder. Finally, after converting the data to a string, it can be parsed into its proper data type or structure.

Aside from writing and reading, you can delete files. This process is even simpler and will remove the entire file and its contents.

// index.js

So with these few methods, you can begin working with the file system in Node. There are many other methods in addition to these that allow you to do many other things like truncate files, open/close files, and even work with directories. You can read more on these methods in the Node documentation. You can also check out this link to a tutorial on Node which was very informative and easy to follow.

Flatiron School alumni and Full Stack web developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store