# Installing Node.js on Raspberry Pi

### <mark style="color:blue;">Introduction</mark>

Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to run JavaScript code outside of a web browser.

It is built on the Chrome V8 JavaScript engine and provides an event-driven, non-blocking I/O model, making it efficient and suitable for building scalable network applications.

Node.js has gained popularity among developers for its ability to handle high-throughput and real-time applications. It is commonly used for building server-side applications, command-line tools, and even desktop applications using frameworks like Electron.

In this guide, we will walk through the steps to install Node.js on a Raspberry Pi.

### <mark style="color:blue;">Prerequisites</mark>

Before proceeding with the installation, ensure that you have the following:

* Raspberry Pi (any model)
* Raspberry Pi OS (formerly known as Raspbian) installed on your Raspberry Pi
* Internet connection on your Raspberry Pi

### <mark style="color:blue;">Step 1: Update and Upgrade Packages</mark>

First, let's make sure your Raspberry Pi is up to date.&#x20;

Open a terminal and run the following commands to update the package list and upgrade installed packages:

```bash
sudo apt update
```

```bash
sudo apt upgrade
```

### <mark style="color:blue;">Step 2: Install Required Dependencies</mark>

To set up the Node.js repository and install Node.js, we need a few dependencies. Run the following command to install them:

```bash
sudo apt install -y curl gnupg
```

### <mark style="color:blue;">Step 3: Add Node.js Repository</mark>

Node.js provides an official repository for easy installation on Debian-based systems like Raspberry Pi OS.&#x20;

Follow these steps to add the repository:

#### <mark style="color:green;">Import the Node.js GPG key</mark>

```bash
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
```

This command downloads and runs a setup script that adds the Node.js GPG key and repository to your system.&#x20;

Note that `setup_18.x` refers to the latest LTS (Long-Term Support) version of Node.js. You can replace `18.x` with the desired version number.

#### <mark style="color:green;">Update the package list again to include the newly added repository</mark>

```bash
sudo apt update
```

### <mark style="color:blue;">Step 4: Install Node.js</mark>

Now that the Node.js repository is set up, you can install Node.js using the following command:

```bash
sudo apt install -y nodejs
```

This command installs Node.js and npm (Node Package Manager), which is used for managing Node.js packages and dependencies.

### <mark style="color:blue;">Step 5: Verify Installation</mark>

To verify that Node.js and npm are installed correctly, run the following commands:

```bash
node -v
```

```bash
npm -v
```

These commands will display the installed versions of Node.js and npm, respectively.

### <mark style="color:blue;">Step 6: (Optional) Install Build Tools</mark>

Some Node.js packages may <mark style="color:yellow;">require compiling native code.</mark>&#x20;

To ensure that you have the necessary build tools, you can install the <mark style="color:yellow;">`build-essential`</mark> package:

```bash
sudo apt install -y build-essential
```

This package includes the required compilers and libraries for building Node.js packages with native dependencies.

### <mark style="color:blue;">Conclusion</mark>

Congratulations! You have successfully installed Node.js on your Raspberry Pi.

&#x20;You can now start developing and running Node.js applications on your Raspberry Pi.

To get started, you can create a new file with a `.js` extension and write your Node.js code.&#x20;

For example, create a file named `app.js` with the following content:

```javascript
console.log("Hello, Node.js on Raspberry Pi!");
```

Then, open a terminal, navigate to the directory where you saved the file, and run the following command:

```bash
node app.js
```

You should see the message "Hello, Node.js on Raspberry Pi!" printed in the terminal.

Feel free to explore the vast ecosystem of Node.js packages and frameworks to build powerful applications on your Raspberry Pi. Happy coding!
