Sometimes, we want to append to new line in Node.js.
In this article, we’ll look at how to append to new line in Node.js.
How to append to new line in Node.js?
To append to new line in Node.js, we can use the os.EOL
constant.
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 log the log.txt file with append permission with fs.open
.
In the fs.open
callback, we call fs.write
to add the os.EOL
end of line character after the text
.
Then we call fs.close
in the write
callback to close the file.
Conclusion
To append to new line in Node.js, we can use the os.EOL
constant.