How to install Node.js on Raspberry Pi

A Raspberry Pi can function as a lightweight web server, hosting and serving content to thousands of users for a very low cost. Raspberry Pi models can host web server environments such as Node.js, which is a JavaScript runtime environment that is used for hosting websites. It offers users the ability to write websites in JavaScript, in which the code executes on the server instead of a client’s browser. This puts some additional load on the server when compared to a static site, but a Raspberry Pi can easily handle light traffic to the site.

To host a website with Node.js on a Raspberry Pi system, you need to download and configure the Node.js software. Node.js is available for installation from the default repositories, and can be installed alongside npm, the package manager for Node.js and JavaScript. Both packages go hand in hand and give us the tools needed to host our Node.js website. In this tutorial, you will learn how to install Node.js and npm on a Raspberry Pi to get started hosting your JavaScript website.

In this tutorial you will learn:

  • How to install Node.js on a Raspberry Pi
  • How to host an example website with Node.js
  • How to access Node.js website in browser
How to install Node.js on Raspberry Pi
How to install Node.js on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software Node.js, npm
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install Node.js on Raspberry Pi step by step instructions




Below we will go through the step by step instructions to install the Node.js package on a Raspberry Pi. Then, we will host an example “Hello World” website so we can verify that our installation was successful and is working as expected. From there, you can import your Node.js site to the Raspberry Pi and start hosting it immediately, or get started with coding your website.

  1. Get started by opening a command line terminal and executing the two commands below to update your package repository cache and install the nodejs and npm software packages:
    $ sudo apt update
    $ sudo apt install nodejs npm
    
    Many Node.js related packages will be installed with this command
    Many Node.js related packages will be installed with this command

    Once Node.js is installed, you can begin using it to host your website, along with using npm to install any necessary JavaScript packages.

  2. If you do not already have a Node.js website ready to go, then we can launch a simple “Hello World” test website just to see that Node.js was installed correctly and that we are able to host our JavaScript website and access it from browser. Start by creating a new file named app.js and pasting the following content in it:


    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    Creating a simple Hello World test website in Node.js
    Creating a simple Hello World test website in Node.js

    Save your changes and exit the file when done.

  3. Next, run the application by executing:
    $ node app.js
    
  4. Use Chromium or your preferred browser to check out the website, which will be hosted and accessible at http://localhost:3000 by default.
    Checking out our Node.js website that is being hosted on a Raspberry Pi
    Checking out our Node.js website that is being hosted on a Raspberry Pi

Closing Thoughts




In this tutorial, we saw how to install Node.js on a Raspberry Pi system. We also learned how to install the npm package manager, and how to start hosting a website with Node.js. In case you do not already have a website ready to publish, we can use the Hello World example provided here to at least get a Node.js site up and running. From there, feel free to edit the script to fit your needs (JavaScript knowledge is required) or import your own website to the Raspberry Pi.



Comments and Discussions
Linux Forum