Why does Serial.readStringUntil('\n') not work with node.js?

I'm using Serial.readStringUntil() to try to grab a string from the serial buffer. I use a newline ('\n') at the end of my string to signify when I'm done.

Here is my Arduino code

void setup() {
  Serial.begin(9600);
}

void loop() {
  //get serial input
  while(Serial.available() > 0){
    String input = Serial.readStringUntil('\n');
    Serial.println("I received " + input);
    if(input == "foo"){
      //do stuff...
    }
  }
  delay(1);
}

The code executes fine when I use the arduino IDE's serial monitor, however when I use the following code from my node.js app, the readStringUntil() function seems to skip right over my endline, and keeps looking until it hits timeOut. After it hits timeOut, the input string's value is as I would expect it be ("foo").

const SerialPort = require('serialport')

var port;

SerialPort.list().then(function(portInfo){ //list all the ports
	port = new SerialPort(portInfo[1].comName, { //connect to the 2nd one (I know this is my device)
	baudRate: 9600,
    	dataBits: 8,
    	parity: 'none',
    	stopBits: 1,
    	flowControl: false,
	}, function (err) {
		if (err) { //if there was an error connecting
			return console.log('Error: ', err.message) //log it
		}


		setInterval(function(){ //every 100 miliseconds
			sendMessage(); //fire the function "sendMessage"
		},100);
	});
});

function sendMessage()
	var message = "foo\n";
	port.write(message, function(err) {
		if (err) {
			return console.log('Error on write: ', err.message); //if there was an error sending the message, log it
		}
	});
}

In the above example, the arduino never seems to finish reading the serial input. What's confusing to me is that when I switch my node app to the code below (once the arduino outputs data with Serial.println, wait 10 miliseconds then send another message), the arduino catches the endline and sends a message back immediately.

const SerialPort = require('serialport')
const Readline = require('@serialport/parser-readline');

var port;

SerialPort.list().then(function(portInfo){
	port = new SerialPort(portInfo[1].comName, {
	    baudRate: 9600,
    	dataBits: 8,
    	parity: 'none',
    	stopBits: 1,
    	flowControl: false,
    	parser: new Readline()
	}, function (err) {
		if (err) {
			return console.log('Error: ', err.message)
		}

		const parser = port.pipe(new Readline({ delimiter: '\r\n' }));

		sendMessage();
		// Switches the port into "flowing mode"
		parser.on('data', function (data) {
			setTimeout(function(){
				sendMessage();
			},10);
		});
		
	});
});

function sendMessage(){
	var message = "foo\n";
	port.write(message, function(err) {
		if (err) {
			return console.log('Error on write: ', err.message);
		}
	});
}

Can somebody give me some enlightenment into why this is happening?

Does the Arduino code work properly if you input the data using the Serial monitor ?

Yes sir (so long as I have the "newline" setting enabled)

isaacO:
Yes sir (so long as I have the "newline" setting enabled)

So the Arduino code is working. Maybe your PC program is not sending what you think it is

Do you by any chance have the Serial monitor window open when receiving data from the PC ?
Have you tried using SoftwareSerial (using pins other than 0 and 1) ?

How long does the node.js program wait before it sends the first message?

If it opens the port, and immediately sends, the board would still be running the bootloader (assuming it's a typical AVR-based Arduino), since opening a port results in a board reset (through the DTR autoreset circuit, which is what allows you to upload a sketch without a carefully timed press of reset button).

DrAzzy:
How long does the node.js program wait before it sends the first message?

If it opens the port, and immediately sends, the board would still be running the bootloader (assuming it's a typical AVR-based Arduino), since opening a port results in a board reset (through the DTR autoreset circuit, which is what allows you to upload a sketch without a carefully timed press of reset button).

Success! I think that was the problem.

I tried adding a 5 second delay upon startup until I sent my first messages, and now it's working like a charm. Thanks everybody for the help!