Node.js is a trademark of Joyent, Inc. and is used with its permission. We are not endorsed by or affiliated with Joyent.
Raspberry Pi is a trademark of Raspberry Pi Ltd. We are not endorsed by or affiliated with Raspberry Pi Ltd.
Open-Meteo APIs are free for open-source developers and non-commercial use. We are not endorsed by or affiliated with Open-Meteo.
Quick start (tl;dr)#
To install Node.js on your Raspberry Pi, run the following commands:
I've included more details below if you'd like to learn about Node.js, the installation procedure, or how to run an example application.
Table of contents#
What is Node.js?#
npm instead of
pip as your library management tool.
The strength of Node.js, especially as it relates to HTTP performance, stems from its event loop and non-blocking I/O. Reading and writing to a database are I/O intense operations that can take time to complete. Some frameworks use blocking I/O which prevents the program from performing other work until the I/O completes. Node.js makes every attempt to avoid blocking I/O.
Non-blocking I/O operations in Node.js are given a callback function. You can think of a callback function as a set of instructions that are run after an operation is complete. While the I/O is being performed, Node.js is free to take on another task. The I/O will eventually complete and the callback function it was given is then executed, all orchestrated by something called an event looper. You can read more about the Node.js event looper on nodejs.org.
If this is a little obscure, don't worry. You don't need to understand how Node.js works under the hood in order to use it. Event loops and non-blocking I/O are a lecture-worthy topics that I'll cover in detail someday. For now, let's get you setup with Node.js on your Pi.
At the time of writing, Node.js is not bundled with the default Raspberry Pi image which means we have to install it ourselves. According to nodejs.org, you can install Node.js on your Raspberry Pi by adding the NodeSource Personal Package Archive (PPA) and then running
I chose to use Node.js version 16, the latest Long Term Support (LTS) version at the time of writing. The following installation procedure was taken from the NodeSource GitHub page3.
To add the NodeSource PPA, run the following command on your Pi:
After the NodeSource PPA is added, install Node.js by running the command:
Verify your installation completed successfully by running:
which should repeat back the version of Node.js that was installed.
Running an example application#
Now that you have Node.js installed on your Raspberry Pi, let's write a small application that pulls weather data from open-meteo.com.
First, we need to create a new Node.js project. Run the following commands on your Raspberry Pi to create a new project called `nodejs-weather`:
To grab weather data, we're going to make an HTTP GET request to the Open-Meteo API. A popular library for making HTTP requests is axios, which we can install with the following command:
npm installin the root of your `nodejs-weather` folder.
We'll provide latitude and longitude to Open-Meteo for weather data. I selected the coordinates of NASA's Goddard Space Flight Center to test with but you can update the code with your location.
Your browser can update the example code with your approximate coordinates by clicking the button below. Your location won't be visible to anyone else viewing this blog post and it won't be shared with us.
Create an empty file called `main.js` and paste the following code into it:
Make sure you save the changes to `main.js`. You can run the code using the command:
which should give you an output similar to
Congratulations, you now have a relatively inexpensive weather monitor! I hope this tutorial helped you start your Node.js + Raspberry Pi journey.
Sign up for blog updates#
Be the first to know about new blog posts from Persanix™ with notifications sent directly to your inbox. No spam, seriously.
- About Node.jsnodejs.org
- Installing Node.js via package managernodejs.org
- NodeSource Node.js Binary Distributionsgithub.com/nodesource