Language: EN

como-instalar-nodejs-en-raspberry-pi

How to install Node.js on Raspberry Pi

Node.js is a runtime environment for JavaScript based on the V8 engine from Google Chrome. It allows you to run JavaScript code on the server, making it easier to develop web and network applications without needing to use another language on the backend.

Some of the key features of Node.js are:

  • Asynchronous and Non-Blocking: Node.js uses a non-blocking I/O model, allowing it to handle multiple connections simultaneously without one operation interfering with another.
  • Event-Driven: It uses an event-driven system to manage operations and respond to events, making it ideal for real-time applications.
  • Package Management: Node.js comes with npm (Node Package Manager), which makes it easy to install and manage packages and dependencies.
  • Scalability: It is ideal for applications that require high scalability and network performance.

Having Node.js on a Raspberry Pi can serve many purposes, from a web server to an IoT device. Using Node.js on the Raspberry Pi.

For example, you can develop web applications and APIs directly on your Raspberry Pi, create scripts to automate tasks, or control devices connected to the Raspberry Pi. Or implement Internet of Things projects that require real-time interaction with devices and online services.

If you need more information about Node.js, here’s a link to the course 👇

How to Install Node.js on Raspberry Pi

Make sure your Raspberry Pi is updated before starting the installation. Run the following commands to update the packages and the operating system:

sudo apt update
sudo apt upgrade

NodeSource is a repository that provides updated versions of Node.js that can be easily installed on Raspberry Pi. We will use their repository to install Node.js.

To add the NodeSource Repository, we first need to install curl if it is not already installed:

sudo apt install curl

Then, use curl to download the installation script for the NodeSource repository:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

Note: The 20.x in the URL refers to the version of Node.js we are installing. You can change it to the version you prefer.

After adding the repository, install Node.js with the following command:

sudo apt install -y nodejs

This will install both Node.js and npm (Node Package Manager).

Verify the Installation

Once the installation is complete, you can verify that Node.js and npm have been installed correctly with the following commands:

node -v
npm -v

These commands should display the version of Node.js and npm that you have installed.

Develop a Simple Application

To test that everything is working, we can create a basic example. Create a file named app.js:

nano app.js

Then, add the following code to the app.js file:

import { createServer } from 'node:http';

const server = createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello from LuisLlamas.es!\n');
});

// starts a simple http server locally on port 3000
server.listen(3000, '127.0.0.1', () => {
  console.log('Listening on 127.0.0.1:3000');
});

Open a web browser and visit http://localhost:3000. You should see the message “Hello from LuisLlamas.es!“.