Install Node SerialPort on Raspberry Pi
·
Timo Denk
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.

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.

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);
});