Search code examples
javascriptnode.jslinuxforeachraspbian

Node.js .forEach output not as expected in Raspbian but fine in windows


The code is identical across platforms but when running my code in windows it performs as expected and returns:

test: US
test: us

But then running it in Raspbian which is the only change, it returns:

test:
US
us

I couldn't find any reason why it would behave differently in Raspbian or even just base Linux which is why I am asking here. The code I am used to create these outputs is as follows:


var data = fs.readFileSync('./txt/wordlist.txt', 'ascii').toString().split("\r\n"); 

var filtered = data.filter(function (el) {
  return el != "";
});
        
if(filtered!=""){
  filtered.forEach(function(d){
    var dFS = "test: " + d;
    console.log(dFS);
  });
}

The wordlist I have is as follows with plans to expand of course:

US
us

The method used to create the wordlist for completeness:

added.forEach(function(a){
          fs.appendFileSync('./txt/wordlist.txt',`\r\n${a}`,'ascii');
        });

Solution

  • So it turned out to be a strange format issue in regards to my wordlist both being written to in ascii and read in ascii including using \r\n when writing and splitting the returned output. I realised this was a main different in Linux environments and after changing everything to utf-8 and splitting on only \n and replacing the line breaks in the wordlist it worked as expected.