Categories
JavaScript Answers

How to load file with Node?

Spread the love

To load file with Node, we call the readFile method.

For instance, we write

const fs = require("fs");

fs.readFile(__dirname + "/test.txt", (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data.toString());
});

to call readFile with path of the file to read from.

And we get the file data from data in the callback.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.