Read-Only Mode: Your progress will not be saved, and certain functions are unavailable.Click here to sign upand access the full features of Cratecode.

File Input/Output

By: Cratecode

Now that we know how to do some basic operations on strings and arrays, let's look at how to read and write data from a file. This is a big topic, and so we'll just be looking at the basics.

The first thing we need to look at is the fs library. It's actually built in to Node.js, so we can use it right away, without needing to install anything. It has a bunch of methods to help us read and write files.

Even though it's built-in, we still need to require it to use it. We can do this by writing const fs = require("fs"); at the top of our file.

Once we've done that, we'll want to look at the fs.readFileSync method. There is a readFile method, but it's a little more complicated, so we'll stay away from it until later.

readFileSync takes in two arguments: the path to the file, and an object with some options. We'll just be using the encoding option, which determines how the file should be read. We'll be setting it to "utf8" (a widespread encoding scheme, and the one that your files are probably using). Click here to view the official documentation. Here's an example of how it looks in code (after we've imported fs, of course):

const data = fs.readFileSync("./data.txt", { encoding: "utf8" });

This will read the data.txt file in the same directory as our js file and store it as a string in the data variable.

Let's try writing a few lines of code to read in a file. I've included a data.txt file in this project for you to read. Try reading it in, then printing it to the console. Good luck!

Hey there! Enjoyed the lesson? Consider sharing it with others - it's a huge help and lets us keep making them!

Edit this lesson

Output is unavailable in read-only mode.

AI Assistant


File I/O (Reading)