Raspberry Pi

How to Write and Run Your First Node.js Program on Raspberry Pi

Node.js is an open-source server-side platform that enables users to run Javascript code outside the browser. It helps developers to execute command-line tools and server-side scripting by using javascript. Most famous platforms like Netflix, and Uber use it since it’s a great tool for beginners who want to start developing data incentive applications, including streaming and real-time apps.

This article is an easy guide to write and run your first Node.js program on a Raspberry Pi system.

Write and Run Node.js Program on Raspberry Pi for the First Time?

To write and run the Node.js program on Raspberry Pi, there are some prerequisites which need to be fulfilled and those include installing Node.js and Node Package Manager (NPM) on Raspberry Pi.

So for a complete process of writing and running a Node.js program, follow the steps mentioned below:

Step 1: Update/Upgrade Raspberry Pi Repository

First update the Raspberry Pi repository to get the latest version of packages available in the repository. To check for packages update in the repository, run the below-mentioned command:

$ sudo apt update

Then upgrade the repository using the below-written command:

$ sudo apt upgrade -y

Step 2: Install Node.js on Raspberry Pi

To install the Node.js package on Raspberry Pi, use the following command:

$ sudo apt install nodejs

Step 3: Verifying Installation

After installing the Node.js package, run the below-written command to verify the installation:

$ node --version

The command will display the installed version of node.js as an output:

The NPM is also installed with Node.js and you can confirm it using the following command:

$ npm --version

Step 4: Write First Node.js Program

To write the very first Node.js program, open the nano editor and name your file according to your choice:

Syntax

$ nano <file name>.js

Example

$ nano helloprogram.js

Now inside the file, write your first program to print the message and for that use the following code:

Syntax

console.log("message");

Example

console.log("Hello Linuxhint followers");

Console” is the object in Node.js and to print the desired message/string with Node.js, the log is used:

Then save the file using Ctrl+X.

Step 5: Run the Node.js Program

To run the Node.js program, simply write the below-mentioned command along with the name of your Node.js file that has been created in the previous step:

Syntax

$ node <file name>.js

Example

$ node helloprogram.js

The output will be displayed on the terminal:

In this way, you can write and run different Node.js programs on Raspberry Pi.

Create First Node.js Application and Run it on the Server

After writing the first Node.js program, let’s create a Node.js server-based application and for that, follow the below-mentioned steps:

Step 1: For creating a Node.js web server, let’s create another .js file using nano editor:

Syntax

$ nano <filename>.js

Example

$ nano linuxhint.js

Step 2: Now within the file, import the “http” module and will store the returned HTTP instance in a variable http:

var http = require("http");

Then we will create a server to send a response and print our message on the console. Add the following lines inside the linuxhint.js file:

http.createServer(function (request, response) {
   // Send the HTTP header
   // HTTP Status: 200 : OK
   // Content Type: text/plain
   response.writeHead(200, {'Content-Type': 'text/plain'});
   
   // To send the response body
   response.end('\n');
}).listen(8081);

// to print the message on console
console.log('Server running at http://:8081/');

The user can change the message according to desirability:

Save the file by pressing keys Ctrl+X then Y.

Step 3: Now run the .js file using the following command:

$ node <file name>.js

Step 4: Now open the browser and write the server address that appears in the above command:

http://192.168.18.2:8081/

Note: Add your own Raspberry Pi IP address instead of “192.168.18.2”, which you can find by running the “hostname -I” command. You can add the address on any system browser.

Conclusion

To write the first Node.js program, you have to install Node.js on Raspberry from the official repository of Raspberry Pi. After the installation, by using the nano editor can create the .js file and add the Node.js program into the file and run the file using the node command. You can also create a Node.js server-based application through the same procedure. However, you have to do some configuration within the .js file to access the application on the web using your Raspberry Pi IP address.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.