nodejs cli on raspberry pi post

Finally another update on my personal blog ;-) I've been busy the past year - learning new stuff and improving on existing skills.

So, what did I do? Mostly I've been learning Angular, TypeScript, working on some php projects and improving on EAM and requirements engineering.

Today I want to share some of my learnings I had while working with nodejs on raspberry pi. The objective was to create a backup tool which runs on said raspberry, with a lot of restrictions. Backups had to be downloaded via FTP only, also the backup had to start on an interval only if there's a USB stick present. Maybe use a binary to start everything. Simple, so far.

I started with a lot of research on how to detect the presence of a USB device and on how to mount/manage it. Soon, I came up with a USB detection library that reacts on('add', (device: Device) => {}) and on('remove', (device: Device) => {}). Besides, I had to establish some baseline and reliable setup for working with node and creating cli applications. There my Angular experiences came in handy, since all development is done with TypeScript, I opted to do likewise.

  "main": "./lib/app.js",
  "bin": "./lib/app.js",
  "scripts": {
    "start": "nodemon --watch 'src/**/*.ts' --exec ts-node src/app.ts"
  }

To have fancy colors, argument parsing, etc., I added:

    "arg": "^4.1.1",
    "basic-ftp": "^3.8.7",
    "chalk": "^2.4.2",
    "check-disk-space": "^2.1.0",
    "clear": "^0.1.0",
    "commander": "^3.0.0",
    "dotenv": "^8.1.0",
    "esm": "^3.2.25",
    "figlet": "^1.2.3",
    "fs": "0.0.1-security",
    "node-schedule": "^1.3.2",
    "nodemailer": "^6.3.0",
    "path": "^0.12.7",
    "pino": "^5.13.2",
    "usb-detection": "^4.3.0"

With this basic setup, I could build all I need. Long story short: A lot of challenges came up, costing me loads of time to understand and overcome.

  • if a device is detected, the os needs some time (~4s) to detect it and provide a device name (e.g. /dev/sda). Before that there is no way, the script can do anything useful ...
  • mounting devices on Linux systems can be a bit of pain without root access: pmount did the job
  • ensuring, mounting and unmounting really succeed when using exec and there are no events you can successfully monitor or subscribe on
  • scheduling events is not so easy a if using cronjobs...

Not to forget logging and sending emails on special events like "device capacity below 20%". Another good example on how projects grow beyond the expected features :-)

Categories: programming

Tags: raspberry, nodejs, typescript