Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

09: Files

I am going to show you the simplest way to read something from a file, but this is definitely not the only way nor is it the best way. JavaScript is rather obsessed with many ways to read data, but before you can understand them you need to learn about functions. Functions come later in the book, so for now I'll show you how to just pull in all of a file in one move and then play with it.

The Code

You can read the documentation for the file Application Programmer Interface (API) at https://nodejs.org/api/fs.html, but don't get discouraged if this makes zero sense. Just glance through it and know it exists.

// https://nodejs.org/api/fs.html
const fs = require("fs");

let contents = fs.readFileSync("test.txt");

console.log("Contents:");
console.log(contents.toString());

// using a callback

console.log("----------------------");
fs.readFile("test.txt", (err, data) => {
    console.log(data.toString());
});

The first part of this code has the easy way to read a file using fs.readFileSync(). This just pulls the entire file into your program and puts it into the contents variable.

The second part after the comment // using a callback is the more complicated but normal way that most JavaScript programmers would read a file. This way is much more complex, so I left the description in the video for this exercise.

If you type this code in, and still have no idea what lines 12-14 are doing, then don't despair. We will go over this concept many times in the next exercises.

About require() vs. import

At the time of writing this course (2023) Node.js was in a transition period that might cause this code to become obsolete. If you get an error about "commonjs" or something telling you to change the file name to ex09.cjs, then your node or the way you've configured your project is using the new module system.

You fix this by simple changing this line:

const fs = require("fs");

To the new import syntax:

import fs from "fs";

You later learn about this new syntax, but in the start you use the old syntax so you know how they both work.

What You Should See

Before you run this you need to create a file named test.txt and put it in the same directory as your ex09.js file. I put this in the contents of my file:

I have a problem.
It's not a problem with you.
It's a problem with sleep.
And you have it too.

You can put anything you want in the file, but make sure it is small or you'll have a hard time.

When you run it you should see the file printed twice:

Contents:
I have a problem.
It's not a problem with you.
It's a problem with sleep.
And you have it too.

----------------------
I have a problem.
It's not a problem with you.
It's a problem with sleep.
And you have it too.

You should try to break this file in any way you can. Maybe use the API documentation I mentioned at the beginning.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.