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.

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); });
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
Hi Roberto,
the issues in the GitHub reporsitory could be a useful resource. There might be some people who are seeing the same error messages as you do.
Kind regards,
Timo
Hi Timo Denk
i try install serial port in my rpi3 , but
npm WARN enoent ENOENT: no such file or directory, open ‘/home/pi/package.json’
npm WARN pi No description
npm WARN pi No repository field.
npm WARN pi No README data
npm WARN pi No license field.
How to solve this problem? i need your help
thanks
Hi there, that’s just warnings from npm. No need to worry!
Thanks, Y was experiencing the same problems, then I resolved them just folowing your post.
Thanks, to share your experience.
I followed your instruction and still now working.
pi@raspberrypi:~ $ sudo npm install serialport –unsafe-perm
(node:2360) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
npm ERR! Error: Method Not Allowed
npm ERR! at errorResponse (/usr/share/npm/lib/cache/add-named.js:260:10)
npm ERR! at /usr/share/npm/lib/cache/add-named.js:203:12
npm ERR! at saved (/usr/share/npm/node_modules/npm-registry-client/lib/get.js:167:7)
npm ERR! at FSReqWrap.oncomplete (fs.js:135:15)
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!
npm ERR! System Linux 4.14.71-v7+
npm ERR! command “/usr/bin/node” “/usr/bin/npm” “install” “serialport” “–unsafe-perm”
npm ERR! cwd /home/pi
npm ERR! node -v v8.11.1
npm ERR! npm -v 1.4.21
npm ERR! code E405
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR! /home/pi/npm-debug.log
npm ERR! not ok code 0
Are you using a proxy or something like that? “npm E405” is what you want to search for, I think.
ive followed your instructions hoever after the install my node version is still v4.2.1 any idea what i should do to address this?
Consider looking into the node version manager (https://github.com/creationix/nvm). It’s an extremely helpful tool.