Raspberry Pi 3: Running a Node.js script


In this tutorial we will check how to run a Node.js script on the Raspberry pi 3.

On this previous tutorial we covered how to write a simple “Hello World” program using the Node.js interpreter. Nonetheless, for more complex programs, it’s not feasible to run the commands manually one by one on the interpreter.

Thus, we can write more complex programs on a file and run all of them with Node.js.

For this tutorial, we will write a very simple program that prints to the console the numbers between 0 and 10.

This tutorial was tested on a Raspberry Pi 3 model B+, running version 4.9 of Raspbian, installed using NOOBS.

The procedure

The first thing we will do is creating a file with a .js extension, where we will write our script. We will name our file “test.js“.

Then we will write the actual code inside our file. As mentioned in the introductory section, our program will be as simple as printing the numbers between 0 and 10.

Thus, our code will basically consist on a for loop where we will write the current iteration value to the console:

for(let i=0; i<= 10; i++){

After finishing the code, save the file. Then, open a command line and navigate to the folder where the script file is located. After this, simply write the following command on the command line and hit enter:

node test.js

You should get an output similar to figure 1. As can be seen, the numbers between 0 and 10 were printed, as expected.

Result of the Node.js script, running on the Raspberry Pi 3.
Figure 1 – Result of the Node.js script.

Related Posts

1 thought on “Raspberry Pi 3: Running a Node.js script”

Leave a Reply

%d bloggers like this: