Search code examples
node.jsnpmarduinoraspberry-piserial-port

TypeError: serialport.parsers.readline is not a function error


Hi I'm a newbie to NodeJs. I encountered TypeError: serialport.parsers.readline is not a function error from below code.

    var webSocketUrl = "wss://api.artik.cloud/v1.1/websocket?ack=true";
    var device_id = "########################"; //  DEVICE ID
    var device_token = "#####################"; // DEVICE TOKEN

   // require websocket module to connect 
   // execute following two commands to your pi's terminal
   // sudo apt-get update
   // npm install websocket
  var WebSocket = require('ws');
  var isWebSocketReady = false;
  var data="";
  var ws = null;
 // require serialport module to raspberry pi 
 // execute following command to terminal
 // npm install serialport
 var serialport = require("serialport");
 var SerialPort = serialport.SerialPort;
 var sp = new SerialPort("/dev/ttyACM0", { //for serial communication with 
 arduino 
 baudrate: 9600,  
 // we are using UNO so baudrate is 9600, you might need to change according 
 to your model
 parser: serialport.parsers.readline("\n")
 });

But after i run this i got a error like this

 /home/pi/parking.js:21
 parser: serialport.parsers.readline("\n")
                           ^

TypeError: serialport.parsers.readline is not a function
 at Object.<anonymous> (/home/pi/rainbow-parking.js:21:32)
 at Module._compile (module.js:409:26)
 at Object.Module._extensions..js (module.js:416:10)
 at Module.load (module.js:343:32)
 at Function.Module._load (module.js:300:12)
 at Function.Module.runMain (module.js:441:10)
 at startup (node.js:140:18)
 at node.js:1043:3

Anyone can help me to solve this problem. I suspect the issue is with mismatch my nodejs and npm version.


Solution

  • okay. I found a solution for this problem. The problem was my npm version and serial port versions are not matching with each other. Easily avoid this problem updating those 2 versions same. Thank you everyone for the help