Home > Software design >  NodeJS 'Readline' is not a constructor
NodeJS 'Readline' is not a constructor

Time:05-07

I'm having issues with my NodeJS app. It's supposed to be a NodeJS server to serial port, but I'm having issues with Readline and it used to throw errors about SerialPort too. If anyone can recommend how to syntax check code on node.

The error:

TypeError: readline is not a constructor
at Object.<anonymous> (/x/server.js:17:11)

The code:

const myPort = new SerialPort({
  path:portname,
  baudRate:9600,
  parser: new readline("\n")
}); 
const SerialPort = require('serialport');
const readline = require("readline");

CodePudding user response:

I don't know what you wanted to achieve this way, but you could try:

const myPort = new SerialPort({
  path:portname,
  baudRate:9600,
  parser: readline
}); 

Now you can use readline as a myPort.parser.

If you want to know more about readline, check this out: https://www.geeksforgeeks.org/node-js-readline-module/

CodePudding user response:

Try this with Capital letter

const SerialPort = require('serialport');
const {Readline} = require("readline");

const myPort = new SerialPort({
  path:portname,
  baudRate:9600,
  parser: new Readline("\n")
}); 

  • Related