Skip to main content
Test Double company logo
Services
Services Overview
Holistic software investment consulting
Software Delivery
Accelerate quality software development
Product Management
Launch modern product orgs
Legacy Modernization
Renovate legacy software systems
DevOps
Scale infrastructure smoothly
Upgrade Rails
Update Rails versions seamlessly
Technical Recruitment
Build tech & product teams
Technical Assessments
Uncover root causes & improvements
Case Studies
Solutions
Accelerate Quality Software
Software Delivery, DevOps, & Product Delivery
Maximize Software Investments
Product Performance, Product Scaling, & Technical Assessments
Future-Proof Innovative Software
Legacy Modernization, Product Transformation, Upgrade Rails, Technical Recruitment
About
About
What's a test double?
Approach
Meeting you where you are
Founder's Story
The origin of our mission
Culture
Culture & Careers
Double Agents decoded
Great Causes
Great code for great causes
EDI
Equity, diversity & inclusion
Insights
All Insights
Hot takes and tips for all things software
Leadership
Bold opinions and insights for tech leaders
Developer
Essential coding tutorials and tools
Product Manager
Practical advice for real-world challenges
Say Hello
Test Double logo
Menu
Services
BackGrid of dots icon
Services Overview
Holistic software investment consulting
Software Delivery
Accelerate quality software development
Product Management
Launch modern product orgs
Legacy Modernization
Renovate legacy software systems
Cycle icon
DevOps
Scale infrastructure smoothly
Upgrade Rails
Update Rails versions seamlessly
Technical Recruitment
Build tech & product teams
Technical Assessments
Uncover root causes & improvements
Case Studies
Solutions
Solutions
Accelerate Quality Software
Software Delivery, DevOps, & Product Delivery
Maximize Software Investments
Product Performance, Product Scaling, & Technical Assessments
Future-Proof Innovative Software
Legacy Modernization, Product Transformation, Upgrade Rails, Technical Recruitment
About
About
About
What's a test double?
Approach
Meeting you where you are
Founder's Story
The origin of our mission
Culture
Culture
Culture & Careers
Double Agents decoded
Great Causes
Great code for great causes
EDI
Equity, diversity & inclusion
Insights
Insights
All Insights
Hot takes and tips for all things software
Leadership
Bold opinions and insights for tech leaders
Developer
Essential coding tutorials and tools
Product Manager
Practical advice for real-world challenges
Say hello
Developers
Developers
Developers
Software tooling & tips

Build and deploy Node.js projects on Raspberry Pi

Discover the best practices for building and deploying Node.js projects on your Raspberry Pi. Overcome common challenges and make your projects run smoothly with our expert tips and tricks.
Kaleb Lape
|
January 17, 2019
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Last Christmas I received a gift so diabolical it ruined many weekends and kept me up at night. It was a brand new Raspberry Pi Zero.

The possibilities were too endless for my finite brain to handle, so I took the only obvious course of action. I threw it in a drawer and slowly backed away. A couple months later I read an awesome post by fellow double agent Adam Lukens. That post gave me the kick in the pants I needed to roll up my sleeves, dust off the Pi and get to work. For an extra challenge I decided to build my project on top of node. I call it Tot Tunes.

I built a working prototype in just an hour or two and felt like a super hero for a few minutes. Then I discovered a bug. I fixed that, but there were a number of other tiny issues that piled up. What if the power goes out or it gets unplugged? Thus, I built up an arsenal of tools around my little project through a series of learning experiences. I want to share what I’ve learned about not just building Tot Tunes itself. That was easy thanks to Adam’s detailed post. I want to lower the barrier for more people to build awesome things and run them with a high degree of confidence without going through the pain I experienced getting there with my project.

If you would like to follow along and build something as you read through this, you will need a Raspberry Pi with Raspbian installed, a reasonable grasp of javascript basics, and an installation of node. We will do a small amount of shell scripting as well.

I built a node thing 😎

You may have a small project you’ve been itching to build, but if not I have a simple node service just for you. Go here, clone the project, and perform the instructions in the readme to get setup.

How do I deploy? 🤨

The most obvious way to deploy a node project is to install node on your Pi, and just start writing code. Some workflows may be perfectly comfortable with this approach, but I know I quickly missed the power and comfort of my laptop. Another common approach is to code it up on your laptop then copy your code over and run npm install && npm start. You would simply copy the code every time you want to update it and restart the service. Not the worst situation in the world, but I thought there must be another way. For one thing, I was limited to the node version available in Raspbian’s package repositories, which was not ideal since I wanted to use newer node versions for performance and features. A common solution to this problem is to use a tool like nodenv to manage node versions. But I realized that I have a choice. Do I continue managing all this infrastructure on the Pi or try to find a simpler way? I had recently heard about a project for bundling up packages with the node runtime so that you could distribute the application as a standalone executable. This ended up solving a lot of my problems. You can find the tool on GitHub at zeit/pkg. Since I was dealing with a binary executable now, I knew I needed to get a binary for linux that would work on the Pi’s CPU architecture. The initial test showed that pkg theoretically supports these constraints, but there was an error message.

Error! Not able to build for ‘armv7’ here, only for ‘x64’

After some digging, I found this issue comment on github. It showed that as long as my dependencies are pure javascript, I could tell pkg not to compile the result to bytecode with a flag, pkg ... --no-bytecode. The build succeeded, so I copied the binary to my Pi. It worked! I no longer had to worry about keeping the deployed and development versions of node in sync. I could just check that configuration into git.

It works! But I think it just crashed… 😱

I live near tornado alley. Fortunately I’ve never actually had a tornado in my neighborhood. Unfortunately every year without fail a storm comes through and the power goes out. Sometimes it’s just a brownout where the power fluctuates a bit, but either way my battery-less Raspberry Pi reboots without warning. The point is that my Raspberry Pi services need to be ready for anything nature throws their way. Since I was using Raspbian, systemd seemed like the right tool for the job. I had no idea how to write a systemd service, but this seemed like the perfect excuse to learn. It’s actually very easy to make! The CLI is pretty intuitive, and you only have to create a single file describing how to run your service.

[Unit]
Description=Tot Tunes

[Service]
User=pi
ExecStart=/usr/local/bin/tot-tunes
Type=simple
Restart=always

[Install]
WantedBy=multi-user.target

Place that file in the /etc/systemd/system/ directory. Then run the following commands.

systemctl daemon-reload
systemctl enable tot-tunes.service
service tot-tunes start

With this simple configuration you get a rock solid service that automatically restarts on reboots and crashes. As a bonus, it also has easy access to your application’s logs through the journalctl interface.

Mission accomplished? 🚀

Hacking on Raspberry Pi projects is fun and a little addictive. But if you’re like me, we also aspire to have a life outside of our little IoT overlords, so there’s not always time to spend hours learning a new operating system or programming language.

We need to get an idea implemented quickly so we can get on with our lives with just a pinch more JS automation because you can never have too much JS 😉.

Although I chose node partly as a fun challenge, I think it has other advantages for IoT newcomers. For one thing, just about everybody knows a little JS, so it’s a way for those folks to get productive fast. If you’re not a fan of JS, there is a ton of tooling available to help you write TypeScript, coffeescript, and many other languages that compile down to JS. npm has thousands of packages so you can build on others’ work, and pkg allows us to package all of this up as an executable binary with no requisite dependencies to install.

Even with all the reasons to use node and pkg on your next Raspberry Pi project, you will want to weigh the costs and benefits before committing to this platform. Node is a pretty heavy dependency, and including the runtime in your binary currently creates files upwards of 30 MB. This can slow down deployments since you have to transfer the entire binary instead of just a few text files. Also, like any interpreted language, you may experience unacceptable performance for your application.

This setup is great for spinning up a simple service, but I think we can continue improving the Raspberry Pi and node experience. One of the biggest dichotomies when building software for the Raspberry Pi is how you can get rapid and higher fidelity feedback by developing on the device itself, but you may not have all the nice tools you are used to. If you choose to build the software from the comfort of your laptop, you may experience the pain of waiting for a deployment every time you want to test your code on the Pi itself.

Another hangup is the tedium of setting up the configuration for your service. The manual process of editing files on the Raspberry Pi can easily eat hours of time you could have spent actually building the thing you are passionate about. A related issue is distribution. What if your non-technical friend visits and notices your cool new gadget? What if you built a project for someone else, and you need to update it?

Opportunities abound in this space, and I’m excited to continue learning, building, and sharing with you. If you are doing exciting things with the Raspberry Pi and any of this resonated with you, please reach out!

Related Insights

🔗
How Clojure enhances Node.js with isomorphic scripting
🔗
How to test logging in Node.js: practical TDD tips and tricks

Explore our insights

See all insights
Developers
Developers
Developers
C# and .NET tools and libraries for the modern developer

C# has a reputation for being used in legacy projects and is not often talked about related to startups or other new business ventures. This article aims to break a few of the myths about .NET and C# and discuss how it has evolved to be a great fit for almost any kind of software.

by
Patrick Coakley
Leadership
Leadership
Leadership
Turning observability into a team strength without a big overhaul

By addressing observability pain points one at a time, we built systems and practices that support rapid troubleshooting and collaboration.

by
Gabriel Côté-Carrier
Developers
Developers
Developers
Why I actually enjoy PR reviews (and you should, too)

PR reviews don't have to be painful. Discover practical, evidence-based approaches that turn code reviews into team-building opportunities while maintaining quality and reducing development friction.

by
Robert Komaromi
Letter art spelling out NEAT

Join the conversation

Technology is a means to an end: answers to very human questions. That’s why we created a community for developers and product managers.

Explore the community
Test Double Executive Leadership Team

Learn about our team

Like what we have to say about building great software and great teams?

Get to know us
Test Double company logo
Improving the way the world builds software.
What we do
Services OverviewSoftware DeliveryProduct ManagementLegacy ModernizationDevOpsUpgrade RailsTechnical RecruitmentTechnical Assessments
Who WE ARE
About UsCulture & CareersGreat CausesEDIOur TeamContact UsNews & AwardsN.E.A.T.
Resources
Case StudiesAll InsightsLeadership InsightsDeveloper InsightsProduct InsightsPairing & Office Hours
NEWSLETTER
Sign up hear about our latest innovations.
Your email has been added!
Oops! Something went wrong while submitting the form.
Standard Ruby badge
614.349.4279hello@testdouble.com
Privacy Policy
© 2020 Test Double. All Rights Reserved.