Install Node SerialPort on Raspberry Pi

Since I had a lot of trouble installing the npm package serialport on my Raspberry Pi B Rev 1 (and Rev 3), I want to share how it eventually worked.

When installing serialport I ran into a lot of errors.
When trying to install using npm install serialport you might see those errors.

Enable serial port usage by adding  enable_uart=1 to the file /boot/config.txt.

If the normal  npm install serialport doesn’t work for you try the following.

Install Node 6.x

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs 
sudo apt-get install -y build-essential

Install node-gyp and node-pre-gyp globally

sudo npm install -g node-gyp
sudo npm install -g node-pre-gyp
sudo npm install serialport --unsafe-perm

Open a terminal to the folder  node_modules/serialport and build the downloaded serialport module with the command  sudo node-gyp configure build.

Serialport build success
“gyp info ok” tells you that it worked!

Run a test script (source) to see whether the installation was successful.

var SerialPort = require('serialport');
var port = new SerialPort('/dev/tty-usbserial1');
 
port.on('open', function() {
  port.write('main screen turn on', function(err) {
    if (err) {
      return console.log('Error on write: ', err.message);
    }
    console.log('message written');
  });
});
 
// open errors will be emitted as an error event 
port.on('error', function(err) {
  console.log('Error: ', err.message);
});

Published by

Timo Denk

Software developer at SAP and Denk Development, student of Applied Computer Science at Baden-Württemberg Cooperative State University. Interested in programming, math, microcontrollers, and sports.

2 thoughts on “Install Node SerialPort on Raspberry Pi”

  1. Hi Timo denk

    I try install serialport in my rpi2 but it not work. I do it all steps that you show in this article but always fail the instalation

    I am using nodejs v6.10.2 and npm 3.10.10

    Do you have a sugerences? I need help.
    thank

Leave a Reply

Your email address will not be published. Required fields are marked *