Automatically run Electron application at reboot on Raspberry Pi

Here is a quick  way to have an application built on electron run at boot on a Raspberry Pi. This worked for me running Raspian Stretch with Desktop.

Edit /home/pi/.config/lxsession/LXDE-pi/autostart with nano:

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Add the following line:

@npm start --prefix /path/to/project/

The file should now look somewhat like this:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@xscreensaver -no-splash
@point-rpi
@npm start --prefix /path/to/project/

Save and exit nano and reboot. Your app should open after the desktop environment loads. Yay!

If you want to be able to get access to the terminal output of your application, install screen with:

sudo apt-get install screen

And then swap:

@npm start --prefix /path/to/project/

For:

@screen -d -m npm start --prefix /path/to/project/

In the above code snippets.

After the pi boots, you can run screen -list to see what screens are available to attach to then attach to yours with screen -r yourscreen. Here’s an example:

Press enter, and then see your terminal output.
For more info on how to use screen, check out this link:

https://www.gnu.org/software/screen/manual/screen.html

Electron cannot be started from an SSH session

Update: If you run export DISPLAY=:0 in the terminal prior to npm start, the application runs just fine on the remote device. Thank you Alex!

https://twitter.com/alexbragdon/status/915601277752573954


In working on an project for work, I have figured out the hard way that Electron has to be started from a terminal session on your target device (ie the computer it is to be viewed on). I am developing an embedded system based on the Raspberry Pi that does not take user input but displays information on a screen.

Upon downloading the electron-quick-start example, everything installs correctly without error and can be done remotely via SSH. Upon running with npm start, the following error is thrown.

> electron-quick-start@1.0.0 start /home/pi/electron-quick-start
> electron .

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! electron-quick-start@1.0.0 start: `electron .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the electron-quick-start@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2017-10-04T15_11_16_398Z-debug.log

I spent most of the evening trying to debug npm ERR! code ELIFECYCLE to no avail. On a lark, I connected a keyboard to the device and ran npm start and it ran without error. Sigh.

The remote development alternative for doing this is to use Remote Desktop Connection a client comes bundled in with windows. The software can be installed on the remote system (the Raspberry Pi) using apt-get install xrdp. Upon connecting, opening up a shell in the RDP client, and running npm start, the example application works just fine.