DEV Community

Cover image for Node Js File System
sstores
sstores

Posted on • Updated on

Node Js File System

Javascript as a coding language was originally created to run in a browser. Node.js can allow javascript to run outside the browser, including on a web server. Node.js is an important tool for javascript and has many modules that hold additional functionality. Before we get into the Node.js File System module, let’s clarify some basics.

What is Node.js?

According to the Node js docs, “Node.js is an open-source, cross-platform, JavaScript runtime environment.” That means that Node.js allows us to run javascript outside of a browser. We can also think of it as a javascript interpreter. “As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications.” Node allows developers to do more than they would be able to do with just vanilla javascript and additionally allows for more add-on functionality including working on both the front-end and back-end of web applications.

What is a Node.js module?

Modules are analogous to javascript libraries, according to W3 School, ” a set of functions you want to include in your application.” The fs module is responsible for all the asynchronous or synchronous file operations. Explained well on the Turing School website, “While working in Node.js, we will encounter 3 different kinds of modules: internal modules that ship with Node, modules that we as developers create, and remote modules that we can download and use from the internet.” Node is a relatively small library because it is easy to customize and to add remote modules.

What is Node.js FileSystem?

FileSystem(fs) is an included internal module of Node.js. According to W3 School, “The Node.js file system module allows you to work with the file system on your computer.” It allows for asynchronous or synchronous access to file structures and is often also used in conjunction with the http Node module. To use fs you must require it in each file it will be used with the following line:

Alt Text

There are many methods in the fs module but some common ones are those to read, write, delete, append information to files in memory. We will go over some very common useful fs methods now:

fs.readFile()

The fs.readFile method allows you to asynchronously ready the entire contents of the given file. The first parameter is the path of the file to be read. The second parameter is an optional options object. The third parameter is a callback function which takes an error and data which is the contents of the file.
Alt Text

fs.writeFile()

The fs.writeFile method takes in four parameters, one of which is optional. The first is the file path of the file to be written, replacing the file if it already exists. The second parameter is the data to be written in the form of a string or a buffer (which we will discuss in just a moment). The third parameter is an optional options object. The last parameter is a callback which takes in an error.

Alt Text

fs.unlink()

The fs.unlink method is used to asynchronously remove a file or “symbolic link”. It takes in a file path and a callback similarly to those we’ve discussed before. This method will work on files but will not work on directories. To delete or unlink a directory you should use
Alt Text

fs.appendFile()

The fs.appendFile method asynchronously appends data to a file, creating the file if it does not already exist. The first parameter is the file path. The second parameter is the data to be appended in the form of a string or a buffer. The third parameter is an optional options object and the last is our callback which takes in an error.
Alt Text

Now, what is a buffer? A Buffer is a class in Node.js which is designed to handle and translate raw binary data. Buffers are usually used in the context of binary data from a stream. A stream is a way of handling data, potentially large amounts of data, and is used to read or write input into output sequentially. In other words, this allows data to be read in small chunks so the entire file can be processed continually with no interruption for the user, and does not need to be stored in memory. We can think of an example in Netflix and other streaming services. These process a huge amount of data and do not require a user to download all the data at once in order to utilize the service. This is just one of the benefits and uses of streams.

There are many more methods in the Node.js file system module than those listed above. The Node.js docs has categories for dealing with asynchronous, synchronous, callbacks, promises and more. FileSystem is a highly useful Node.js module and is worth some exploration.

Sources:

https://www.w3schools.com/nodejs/nodejs_filesystem.asp
https://www.tutorialsteacher.com/nodejs/nodejs-file-system
https://www.w3schools.com/nodejs/nodejs_modules.asp
https://nodejs.org/api/fs.html
https://nodesource.com/blog/understanding-streams-in-nodejs/
https://nodejs.org/en/knowledge/advanced/buffers/how-to-use-buffers/
https://teamtreehouse.com/library/what-is-nodejs
https://frontend.turing.io/lessons/

Top comments (0)