Categories
JavaScript Answers

How to append to New Line in Node.js?

Spread the love

To append to New Line in Node.js, we use some file methods.

For instance, we write

const os = require("os");

const processInput = (text) => {
  fs.open("H://log.txt", "a", 666, (e, id) => {
    fs.write(id, text + os.EOL, null, "utf8", () => {
      fs.close(id, () => {
        console.log("file is updated");
      });
    });
  });
};

to call fs.open to open the file.

Then we call fs.write to write the file with text and the os.EOL new line character.

We then close the file with fs.close after writing to it with the id.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published. Required fields are marked *