127

I'm trying to Append data to a Log file using Node.js and that is working fine but it is not going to the next line. \n doesn't seem to be working in my function below. Any suggestions?

function processInput ( text ) 
{     
  fs.open('H://log.txt', 'a', 666, function( e, id ) {
   fs.write( id, text + "\n", null, 'utf8', function(){
    fs.close(id, function(){
     console.log('file is updated');
    });
   });
  });
 }
1
  • 4
    Are you on Windows/using a Windows-based text editor to view your file, and thus need a CRLF pair, \r\n?
    – Phrogz
    Apr 30, 2012 at 13:17

5 Answers 5

200

It looks like you're running this on Windows (given your H://log.txt file path).

Try using \r\n instead of just \n.

Honestly, \n is fine; you're probably viewing the log file in notepad or something else that doesn't render non-Windows newlines. Try opening it in a different viewer/editor (e.g. Wordpad).

2
  • 2
    was viewing in notepad :)
    – FacePalm
    Dec 17, 2013 at 9:41
  • FYI in Notepad++ you can do find all "\\n" replace "\n" with "Extended" search mode selected at the bottom of the dialog.
    – Drew
    Nov 12, 2019 at 19:12
125

Use the os.EOL constant instead.

var os = require("os");

function processInput ( text ) 
{     
  fs.open('H://log.txt', 'a', 666, function( e, id ) {
   fs.write( id, text + os.EOL, null, 'utf8', function(){
    fs.close(id, function(){
     console.log('file is updated');
    });
   });
  });
 }
7
  • 22
    (1/2) Please note that since node.js can run on many different environments, it may be possible to move your application from lets say a windows environment to a linux one. that means if your application is appending logs using os.EOL you will have some lines ending with /r/n (from the time app was running on windows) and then you will have rows ending just with /n (when app runs on linux). This has the potential to give some trouble especially if any automatic parsing of log files is in place. I decided to just use /n.
    – Sharky
    May 24, 2016 at 8:58
  • 4
    (2/2) Nevertheless i upvoted your answer as it would be the correct one, if we lived in a perfect-regulated world.
    – Sharky
    May 24, 2016 at 9:01
  • 1
    I use linux but liked this approach as I might want portability in future use.
    – user3905644
    Aug 1, 2016 at 3:40
  • 1
    This is the best answer because it works crossplatform. Sep 7, 2018 at 19:29
  • Well, I had to package an application that used fs.write with os.EOL into a binary. I used pkg to generate a binary for Windows. When executed, the last written record has a new line appended to it (as expected); but, seems like other Windows applications treat it is a new record. I am hunting for a work-around. Oct 23, 2018 at 5:36
11

use \r\n combination to append a new line in node js

  var stream = fs.createWriteStream("udp-stream.log", {'flags': 'a'});
  stream.once('open', function(fd) {
    stream.write(msg+"\r\n");
  });
6

Alternatively, you can use fs.appendFile method

let content = 'some text';
content += "\n";
fs.appendFile("helloworld.txt", content, (err) => {
    return console.log(err);
});
2
  • how is this useful? Jan 9, 2022 at 9:13
  • @LakshmanPilaka less code, async
    – hamon
    Sep 27, 2022 at 22:32
-3

Try:

var fs =require('fs');

const details=require('./common');
var info=JSON.stringify(details);

const data=fs.writeFileSync('./tmp/hello/f1.txt',`\n${info}`,{'flag':'a'},function(err,data){
    
if(err) return console.error("error",error);
    console.log(data);

});

//steps to exceute 1.Install all the required modules(ie fs is required here). 2.Here (.common) files has json object which i was importing from another file. 3.then import the file and store in details variable. 4.While performing operations json data has to be converted into string format (so JSON.stringify). 5.WriteFileSync (its an synchronous function) 6.once function execution is completed response is returned. 7.store response in data variable and print in console.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.