How to append to a file in Node

0 votes

I am trying to append a string to a log file. However writeFile will erase the content each time before writing the string.

fs.writeFile('log.txt', 'Hello Node', function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
}); // => message.txt erased, contains only 'Hello Node'

Any idea how to do this the easy way?

Jul 20, 2020 in Node-js by kartik
• 37,520 points
100 views

1 answer to this question.

0 votes

Hello @kartik,

you can use appendFile, which creates a new file handle each time it's called:

Asynchronously:

const fs = require('fs');

fs.appendFile('message.txt', 'data to append', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

Synchronously:

const fs = require('fs');

fs.appendFileSync('message.txt', 'data to append');

But if you append repeatedly to the same file, it's much better to reuse the file handle.

Hope it is helpfull!!

Thank you!

answered Jul 20, 2020 by Niroj
• 82,760 points

Related Questions In Node-js

0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, To print stacktrace of Error in console in ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,760 points
135 views
0 votes
1 answer

How to host a Node.Js application in shared hosting?

Hello @kartik, You can run node.js server on a typical ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,760 points
2,863 views
0 votes
1 answer

How to print a stack trace in Node.js?

Hello @kartik, Any Error object has a stack member that traps the ...READ MORE

answered Jul 20, 2020 in Node-js by Niroj
• 82,760 points
544 views
0 votes
1 answer

How to append to New Line in Node.js

Hello @kartik, you're running this on Windows (given ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,760 points
6,960 views
0 votes
1 answer

How to download and install Lavavel framework?

Hey @kartik, First you must have xampp install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,760 points
381 views
0 votes
1 answer

Display Laravel in browser by using cmd promt?

Hello, First you need to have laravel install ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,760 points
202 views
0 votes
1 answer

How can we get started with Laravel through Xampp?

Hii, First you need to start Apache and ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,760 points
184 views
0 votes
1 answer

How to change Laravel official name to any customize name?

Hey, You just need to go Laravel folder through ...READ MORE

answered Mar 17, 2020 in Laravel by Niroj
• 82,760 points
314 views
0 votes
1 answer

How to append to a file in Node?

Hii @kartik, For occasional appends, you can use appendFile, ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,760 points
187 views
0 votes
1 answer

How to update a value in a json file and save it through node.js?

Hello @kartik, It's particularly useful if you're concerned ...READ MORE

answered Oct 11, 2020 in Node-js by Niroj
• 82,760 points
7,685 views