# Node.js Embedded Development on the Raspberry Pi

## Why node.js?

![](https://cdn-learn.adafruit.com/assets/assets/000/021/906/medium800thumb/raspberry_pi_demo.jpg?1448314329)

Why is it&nbsp;worth exploring [node.js](http://nodejs.org/ "node.js")&nbsp;development in an embedded environment? JavaScript is a widely known language that was designed to deal with user interaction in a browser. &nbsp;Thanks to node.js, we can now use JavaScript outside of the browser, and even interact with hardware on devices such as the Raspberry Pi.

Here's a brief overview of some of the features that make node.js great for embedded development.

## npm

[npm](https://www.npmjs.com/ "npm")&nbsp;is one of the best things to happen to node.js. npm is the package manager for node. &nbsp;It makes managing third-party node dependencies a breeze, and makes it easy for mere mortals like myself to publish node.js packages.

Why is this important? &nbsp;Community. Because npm is so great at managing dependencies, you can include&nbsp;open source libraries&nbsp;in your project&nbsp;hassle-free. Since it's easy to include&nbsp;community libraries, the community has grown exponentially, and the number of available packages is&nbsp;mesmerizing. Have&nbsp;you ever tried to install an Arduino library? No offense to Arduino, but it's not the most user friendly experience. Thankfully, they are [working on adding a package manager](https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification)&nbsp;to the Arduino IDE.

## Events

Events&nbsp;are a great way to deal with user interaction. &nbsp;What's an event? &nbsp;Maybe it would be easiest to give you a few examples of events. Toggling a switch, clicking a mouse, and pressing a key on your keyboard are all examples of events.

The great thing about using event listeners is that you can write a chunk of code that will be called whenever an event happens. No longer do you have to constantly check the state of a button to see if the state has changed in a loop. Instead, the button will let you know when it was pressed!&nbsp;

## Streams

The node.js stream API combines the power of events, with the power of the [Unix pipeline](https://en.wikipedia.org/wiki/Pipeline_%28Unix%29). &nbsp;If you know how to pipe commands in Unix environments, you know it's a game changer. We'll dig deeper into streams after taking a look at how to setup the node.js environment on a Raspberry Pi.

# Node.js Embedded Development on the Raspberry Pi

## Connecting via SSH

The Raspberry Pi was designed&nbsp;to make it&nbsp;very easy to get started by connecting a monitor, keyboard, and mouse. &nbsp;But, what if you don't have access to the necessary peripherals or don't want to disconnect&nbsp;them from your main workstation? The answer? [Secure Shell](https://en.wikipedia.org/wiki/Secure_Shell "SSH")&nbsp;(SSH). &nbsp;SSH is a network protocol that can be used for&nbsp;secure&nbsp;remote command-line login, and thankfully it's enabled by default if you are using the&nbsp;[Raspbian Distro](http://www.raspbian.org/ "Raspbian").

## Hardware Connections
![](https://cdn-learn.adafruit.com/assets/assets/000/021/815/medium800/raspberry_pi_pi.jpg?1418831326)

To connect via SSH, you only need connect your Raspberry Pi to power & to your network&nbsp;via an ethernet cable. &nbsp;For this guide, I have loaded a fresh install of the _2014-09-09_ release of Raspbian on my SD card, but if you have an older release of Raspbian already installed, it should work as well.

## Finding Your Raspberry Pi's IP Address

You can plug in an Ethernet cable and the Pi will automagically DHCP and connect to the internet. Then you have to actually connect to the Pi. To find the IP address of your&nbsp;Raspberry Pi, there's a lot of options.

- If you have an HDMI monitor, connect it to the Pi, on boot it will print out it's IP address right before the console login: prompt
- [If you have a console cable, you can use our tutorial to watch the Pi boot and print out the IP address](../../../../adafruits-raspberry-pi-lesson-5-using-a-console-cable)

If you have neither, you can find the device using your router's control panel, or you can use&nbsp;[nmap](https://en.wikipedia.org/wiki/Nmap "nmap Wikipedia Article")&nbsp;to search for devices that are accepting connections on&nbsp;TCP port 22, which is the port SSH uses. &nbsp;I am not going to assume that you have access to your router's control panel, so I will show you how to use _nmap_ to locate your Raspberry Pi's IP address.

If you are running Windows, head&nbsp;over to nmap.org for their [Windows installation instructions](http://nmap.org/book/inst-windows.html "nmap.org Windows Install"). &nbsp;If you are running Mac OS X, you can install using the [executable installer](http://nmap.org/book/inst-macosx.html "nmap Mac OS X Installer"), or by using a package manager like [homebrew](http://brew.sh/ "homebrew OS X package manager").

```auto
brew install nmap
```

If you are running a Linux distribution on your workstation, use your favorite package manager to install nmap, or visit the [nmap.org downloads page](http://nmap.org/download.html "nmap.org Downloads")&nbsp;for instructions on installing from source.

```auto
sudo apt-get install nmap
```

Once you have nmap installed, you are now ready to search for the IP address. &nbsp;Using the terminal on Linux & Mac OS, or the command prompt on Windows, enter the following command.

```auto
nmap -p 22 --open -sV 10.0.1.*
```

If your local network is setup to use **192.168.1.\*** addresses, modify the command to match your network setup.

```auto
nmap -p 22 --open -sV 192.168.1.*
```

The output of nmap might seem intimidating, but here are a couple hints that should make it easier to identify your Raspberry Pi. &nbsp;Look for a line that contains something like "_ **OpenSSH 6.0p1 Debian** _". &nbsp;Once you find that, navigate your way up a few lines&nbsp;to a&nbsp;line that starts with "_ **Nmap scan report for...** _".&nbsp; The IP address that follows that statement should be the IP of your Raspberry Pi! &nbsp;In the example below, you can see that the IP address I'm looking for is **10.0.1.10**.

```auto
$ nmap -p 22 --open -sV 10.0.1.*

Starting Nmap 6.47 ( http://nmap.org ) at 2014-12-17 11:47 EST
Nmap scan report for 10.0.1.10
Host is up (0.0056s latency).
PORT   STATE SERVICE VERSION
22/tcp open  ssh     OpenSSH 6.0p1 Debian 4+deb7u2 (protocol 2.0)
Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel

Service detection performed. Please report any incorrect results at http://nmap.org/submit/ .
Nmap done: 255 IP addresses (3 hosts up) scanned in 3.39 seconds
```

## Connecting via SSH

If you are using Windows, you will need&nbsp;to install a SSH client like [PuTTY](http://www.putty.org/ "PuTTY")&nbsp;to connect to your Raspberry Pi, but if you use Linux or Mac OS X, you are ready to connect. &nbsp;The default user for logging into a fresh Raspbian install is&nbsp; **pi** , and the default password is&nbsp; **raspberry**. &nbsp;If you are using PuTTY on Windows, enter the connection details into the session configuration window.

Replace the **10.0.1.10** address with the IP address of your Raspberry Pi.

![raspberry_pi_putty1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/021/818/medium640/raspberry_pi_putty1.jpg?1418837345)

Under the _Connection -\> Data_ menu, enter&nbsp; **pi** &nbsp;into the _Auto-login username_ field, and click the _Open_ button at the bottom of the window.

![raspberry_pi_putty2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/021/819/medium640/raspberry_pi_putty2.jpg?1418837405)

If you are using Linux or Mac OS X, enter the following command into your terminal.

```auto
ssh pi@10.0.1.10
```

You will then be prompted to verify that you want to connect to the host. &nbsp;Type&nbsp; **yes** to confirm, and hit the _Enter/Return_ key on your keyboard. You should only be prompted to confirm that you want to connect the first time you try connecting.

```auto
$ ssh pi@10.0.1.10
The authenticity of host '10.0.1.10 (10.0.1.10)' can't be established.
RSA key fingerprint is e8:e0:f0:09:7e:a4:81:42:44:30:65:f0:4f:b4:a6:a5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.1.10' (RSA) to the list of known hosts.
```

After that, you will be prompted for the password for the **pi&nbsp;** user. &nbsp;Enter the default password of **raspberry** and hit the Enter/_Return_ key. &nbsp;If you have entered everything correctly, you should be logged into your Raspberry Pi via SSH!

```auto
pi@10.0.1.10's password:
Linux raspberrypi 3.12.28+ #709 PREEMPT Mon Sep 8 15:28:00 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Dec 17 14:01:23 2014 from 10.0.1.7
pi@raspberrypi ~ $ 
```

[Once you get this all working and you can connect to the Pi, its a good idea to install Zeroconf/Bonjour](../../../../bonjour-zeroconf-networking-for-windows-and-linux)so you can just **ssh to raspberrypi.local**

# Node.js Embedded Development on the Raspberry Pi

## Installing node.js

Now that you are connected to your Pi via SSH, you can move on to installing the latest stable version of node.js.

## Adding the Package Repository

The first step will be to add the NodeSource package repository to your Pi's `/etc/apt/sources.list` file. You can do this by running the following command.

```auto
curl -sL https://deb.nodesource.com/setup_10.x > nodesetup.sh

# It's a good idea to inspect scripts before you run them:
nano nodesetup.sh

sudo bash nodesetup.sh
sudo apt-get install -y nodejs
```

Next, we will&nbsp;install the latest version of node.js using _apt-get_.

```auto
sudo apt-get install -y nodejs
```

If everything went as expected, we can check the installed version of node.js by running _ **node -v** _. &nbsp;At the time of this writing, the latest stable&nbsp;version of node.js was _v10.11.0_.

```auto
pi@raspberrypi ~ $ node -v
v10.11.0
```

Now that we have node.js installed, we can look at the strengths of node.js development on a Raspberry Pi.

# Node.js Embedded Development on the Raspberry Pi

## Events & Callbacks

If you are used to programming hardware in an Arduino environment, this section might seem a bit&nbsp;foreign&nbsp;to you. Unless you are using interrupts, code execution happens in logical order. When dealing with events & callbacks, specific chunks of code gets called as events happen. If you are used to developing with Javascript, then you should feel right at home.

## Callbacks

We've already discussed events earlier in the guide, but what's a callback? A callback is a function that is&nbsp;passed as a parameter to&nbsp;another function so it&nbsp;can be used at any point in the future.

What on earth does this have to do with events & event listeners? When dealing with asynchronous&nbsp;events, your callback function&nbsp;defines the actions that happen in response to events. &nbsp;If you want a LED to light up when a button press happens, then you would write&nbsp;the code that turns on the LED inside of a callback. Let's take a look at an example using the Adafruit T-Cobbler to connect the button and LED.

![](https://cdn-learn.adafruit.com/assets/assets/000/024/736/medium800/raspberry_pi_pi_node.png?1429830365)

We will show you how to download and run the example code in the last section of the guide, but if you would like to follow along and create the example files yourself, you will need to install the&nbsp;[onoff](https://www.npmjs.com/package/onoff)&nbsp;npm package.

```auto
npm install onoff
```

Next, you can use&nbsp; **nano** text editor&nbsp;to create the test file. If you need help getting started with&nbsp;nano, [check out this handy guide](../../../../an-illustrated-shell-command-primer/editing-files-nano).

```auto
nano test.js
```

Next, paste the following example into nano, and save the file.

```auto
// button is attached to pin 17, led to 18
var GPIO = require('onoff').Gpio,
    led = new GPIO(18, 'out'),
    button = new GPIO(17, 'in', 'both');

// define the callback function
function light(err, state) {
  
  // check the state of the button
  // 1 == pressed, 0 == not pressed
  if(state == 1) {
    // turn LED on
    led.writeSync(1);
  } else {
    // turn LED off
    led.writeSync(0);
  }
  
}

// pass the callback function to the
// as the first argument to watch()
button.watch(light);
```

From there, you can run the example file using the **node** command. &nbsp;Go ahead run the test file and press the button to make sure that your LED lights up.

```auto
node test.js
```

Let's break down what is happening in the example code. **button.watch(**_callback_**)**&nbsp;is a function that you can call if you would like to watch a button for changes in state. When a change event happens, the **watch()** function calls the _callback_ function that was passed to it as its only parameter. In this case, the watch function will call the **light()** function when the button changes state. &nbsp;

When **watch()** notices that the button state changed, the **light()** function is called with two parameters. &nbsp;The first parameter has any _errors_ that may have occurred, and the second parameter gives the current button _state_. When I wrote the function definition for the light method, I called the first parameter **err** , and the second parameter **state** , but you could name them whatever you would like. It's best to name them after what they represent if possible.&nbsp;

How did I know that the callback would be called with those two parameters? &nbsp;The [documentation for the onoff GPIO library's&nbsp;**watch()** function](https://github.com/fivdi/onoff/blob/master/README.md#usage)&nbsp;demonstrates that it will call the callback with _errors_ as the first parameter and the _button value/state_ as the second.

Callbacks don't have to be defined separately, and then passed to the functions that will call them. You can define the callback and pass it to the watch function all in the same step. The only drawback to this method is that your callback can't be used by any other buttons. &nbsp;Here's an example of defining your callback & passing it to **watch()** in the same step.

```auto
// button is attached to pin 17, LED to 18
var GPIO = require('onoff').Gpio,
    led = new GPIO(18, 'out'),
    button = new GPIO(17, 'in', 'both');

// pass the callback function to the
// as the first argument to watch() and define
// it all in one step
button.watch(function(err, state) {
  
  // check the state of the button
  // 1 == pressed, 0 == not pressed
  if(state == 1) {
    // turn LED on
    led.writeSync(1);
  } else {
    // turn LED off
    led.writeSync(0);
  }
  
});
```

Now that you have a handle on events & callbacks, let's compare how you would approach things in node.js vs how you would approach things in Arduino.

## Arduino & node.js Examples

Let's take a look at how you would approach a couple simple tasks in Arduino, and then compare that with how you would tackle the same tasks using node.js.

Let's start with a classic example. Blinking a LED! First up, Arduino.

```auto
// LED pin
int led = 13;

void setup() {                
  // initialize the LED pin as an output
  pinMode(led, OUTPUT);     
}

void loop() {
  // turn the LED on
  digitalWrite(led, HIGH);
  // delay for one second
  delay(1000);
  // turn the LED off  
  digitalWrite(led, LOW);
  // delay for one second  
  delay(1000);
}
```

What's going on here? As the sketch runs the main loop, it flips the LED on and off separated by delays of one second. Now, let's look at the same example using node.js on a Raspberry PI.

```auto
// export GPIO 18 as an output.
var GPIO = require('onoff').Gpio,
    led = new GPIO(18, 'out');

// start a timer that runs the callback
// function every second (1000 ms)
setInterval(function() {  
  // get the current state of the LED
  var state = led.readSync();
  // write the opposite of the current state to the LED pin
  led.writeSync(Number(!state));
}, 1000);
```

What's going on here? The same thing, but the difference is that the [Arduino delay](http://arduino.cc/en/reference/delay)&nbsp;function blocks all other code from executing for one second for each call to **delay(1000)**, and the&nbsp;[node.js setInterval](http://nodejs.org/api/timers.html#timers_setinterval_callback_delay_arg)&nbsp;timer does not.

Why does this matter? Let's say you wanted to blink a green&nbsp;LED on and off every second, and you wanted to control the state of a red LED with a momentary button.&nbsp;Let's take a look at another Arduino example sketch.

```auto
const int buttonPin = 2;
const int redPin =  10;
const int greenPin = 11;

void setup() {
  // initialize the LED pins as outputs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);      
  // initialize the button pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  
  // read the state of the pushbutton value
  int buttonState = digitalRead(buttonPin);
  // turn the red LED on or off depending on
  // the button state.
  digitalWrite(redPin, buttonState);
  
  // turn the green LED on
  digitalWrite(greenPin, HIGH);
  // delay for one second
  delay(1000);
  // turn the green LED off
  digitalWrite(greenPin, LOW);
  // delay for one second
  delay(1000);
  
}
```

Since we are using Arduino's **delay** function, the button presses could be missed because the loop has to make it through both calls to **delay(1000)** before checking the button state.

**Note:** _I know some of the more experienced Arduino users can think of a ways to avoid using delay(), and solve this problem with interrupts or by checking the current millis() against previous millis() + 1000. This is just a simple example designed to highlight how things can easily get a lot more complicated when you don't have a way to call code&nbsp;asynchronously._

Now let's look at the same thing on a Raspberry Pi using node.js.

```auto
// export GPIO 17 as the red LED output, GPIO 18 as
// the button input, and GPIO green as the button input.
var GPIO = require('onoff').Gpio,
    green = new GPIO(21, 'out'),
    red = new GPIO(17, 'out'),
    button = new GPIO(18, 'in', 'both');

// watch the button for changes, and pass
// the button state (1 or 0) to the red LED
button.watch(function(err, state) {
  red.writeSync(state);
});

// start a timer that runs the callback every second
setInterval(function() {
  // get the current state of the LED
  var state = green.readSync();
  // write the opposite of the current
  // green LED state to the green LED pin
  green.writeSync(Number(!state));
}, 1000);
```

Next, let's look at how to make things even simpler with node.js streams!

# Node.js Embedded Development on the Raspberry Pi

## Streams

Now that we know how to use events, we can dig into one of my favorite parts of the node.js core: readable & writeable streams. &nbsp;

First, we'll walk through some examples of piping in Unix-like environments, since it would be good to know what node.js streams are emulating.

## Unix Pipeline

Let's look at the contexts of an example text file by using the [cat](https://en.wikipedia.org/wiki/Cat_%28Unix%29 "cat command")&nbsp;command to output the contents of the file to [stdout](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29).

```auto
$ cat names.txt 
Gordon
Mike
Brennen
Casey
Toni
Pamela
Nick
Joe
Randy
Ben
Jenny
Tyler
Pete
Chris
Rob
Dave
Jeff
Erik
Paul
```

Now, let's assume we want to reduce the list of names to names with less than four characters. &nbsp;We can pipe the output of **cat** to [sed](https://en.wikipedia.org/wiki/Sed)&nbsp;(with a simple [regex](https://en.wikipedia.org/wiki/Regular_expression)) to accomplish this.

```auto
$ cat names.txt | sed '/.\{4\}/d'
Joe
Ben
Rob
```

Now that we have filtered&nbsp;the list to get the names we were looking for, let's sort the names in reverse dictonary order by piping the output of **sed** to [sort](https://en.wikipedia.org/wiki/Sort_(Unix)).

```auto
$ cat names.txt | sed '/.\{4\}/d' | sort --dictionary-order --reverse
Rob
Joe
Ben
```

Pretty neat, huh? By piping the output of one simple program to the input of another simple program, you can accomplish complex tasks easily & quickly without having to have one&nbsp;behemoth&nbsp;program that reads files, filters, sorts, etc. If you'd like to learn more specifically about piping in Unix environments, check out the [standard IO section](https://p1k3.com/userland-book/#standard-IO)&nbsp;of [Brennen Bearnes' userland book](https://p1k3.com/userland-book).&nbsp;

## Node.js Streams

Now that you have an idea what inspired the stream API, let's try something similar using the node.js stream **pipe()** command.

Let's say we want to view the state of the button from our SSH connection. Since [stdout](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29)&nbsp;is a writable stream, we can pipe the output of the button directly to _stdout_. &nbsp;Let's assume we have a tactile button attached to pin 17 on our Raspberry Pi.

First, install the `gpio-stream` package with `npm`:

```auto
$ npm install gpio-stream
```

Next, save the following code in a file called `stdout.js`:

```auto
var GpioStream = require('gpio-stream'),
    button = GpioStream.readable(17);

// pipe the button presses to stdout
button.pipe(process.stdout);
```

![](https://cdn-learn.adafruit.com/assets/assets/000/021/865/medium800thumb/raspberry_pi_demo2.jpg?1448314311)

That was pretty simple. What if we wanted to redirect the output of our button to light up a LED on pin 18?

```auto
var GpioStream = require('gpio-stream'),
    button = GpioStream.readable(17),
    led = GpioStream.writable(18);

// pipe the button presses to the LED
button.pipe(led);
```

![](https://cdn-learn.adafruit.com/assets/assets/000/021/868/medium800thumb/raspberry_pi_demo3.jpg?1448314323)

## Chunked Transfer Stream

Now that you have a couple simple examples under your belt, let's try something a bit more interesting. &nbsp;What if we wanted to output our button presses to the LED & a web browser?&nbsp;Since the node.js HTTP server response is a writable stream, we can pipe the button presses to the LED, and then to the HTTP response object. Your browser can receive the presses on the fly using [chunked transfer encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding). All of that with ~10 lines of code!

```auto
var GpioStream = require('gpio-stream'),
    http = require('http'),
    button = GpioStream.readable(17),
    led = GpioStream.writable(18);

var stream = button.pipe(led);

http.createServer(function (req, res) {
  res.setHeader('Content-Type', 'text/html');
  res.write('<pre>logging button presses:\n');
  stream.pipe(res);
}).listen(8080);
```

![](https://cdn-learn.adafruit.com/assets/assets/000/021/869/medium800thumb/raspberry_pi_demo.jpg?1448314326)

You could pipe the button presses to a file, a web service, or pretty much anything you can think of thanks to the endless number of stream packages available in [npm](https://npmjs.com). &nbsp;The button example is a simple one, but this same concept can be applied to a wide range of sensors.

Speaking of npm, let's examine how to use npm to manage third party packages. We'll even look at how you can create&nbsp;your own npm package and publish it for everyone to use!

# Node.js Embedded Development on the Raspberry Pi

## Wrapping Things Up

As I described earlier, [npm](https://npmjs.com)&nbsp;is one of the best things about developing in a node.js environment. Because we used a few third party&nbsp;dependencies&nbsp;in the examples, let's take a look at how you would wrap up all of&nbsp;the dependencies and include them in your project by creating a _package.json_ file.

## package.json

The _package.json_ file is what npm uses as a guide when downloading and installing dependencies for&nbsp;your project. The project title, description, version, authors, and third party dependencies&nbsp;are all&nbsp;pieces of information you would find in a standard package file.

Let's start by creating a folder for our project.

```auto
mkdir ~/pi_streams_example && cd ~/pi_streams_example
```

Now, we can use&nbsp; **npm init** to create our _package.json_ file.

```auto
pi@raspberrypi ~/pi_streams_example $ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (pi_streams_example) 
version: (1.0.0) 
description: An example that demonstrates how to use streams with GPIO on a Raspberry Pi
entry point: (index.js) 
test command: 
git repository: 
keywords: raspberry, pi, streams, gpio
author: Todd Treece <todd@uniontownlabs.org>
license: (ISC) 
About to write to /home/pi/pi_streams_example/package.json:

{
  "name": "pi_streams_example",
  "version": "1.0.0",
  "description": "An example that demonstrates how to use streams with GPIO on a Raspberry Pi",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "raspberry",
    "pi",
    "streams",
    "gpio"
  ],
  "author": "Todd Treece <todd@uniontownlabs.org>",
  "license": "ISC"
}


Is this ok? (yes) yes
```

## Dependencies

That took care of creating the basics, but what about adding dependencies? We have a couple of third party dependencies to add to our package: onoff and gpio-stream.

```auto
npm install --save onoff gpio-stream
```

By using&nbsp; **npm install** with the&nbsp; **--save** flag, npm will automatically install & save the dependencies to the package.json file that we just created. &nbsp;All of your dependencies will now be installed in a folder called&nbsp;_node\_modules_ inside your project folder. The best part is that you do not have to worry about including dependencies when you share your code. All someone has to&nbsp;has to do is run&nbsp; **npm install** , and all of the dependencies you listed in your _package.json_ file will automatically be installed via npm for them!

## Example Package

Now that we have looked at how to create a package, let's look at an example package. &nbsp;I have created [a git repository](https://github.com/adafruit/Pi_Node_Example)that contains examples from this guide. Run the following command to clone the repo to your Pi.

```auto
git clone https://github.com/adafruit/Pi_Node_Example.git ~/pi_examples && cd ~/pi_examples
```

Now, to install&nbsp;the dependencies, all you have to do is run&nbsp; **npm install**.

```auto
npm install
```

To run the examples, you can type&nbsp; **node** followed by the name of the example file.

```auto
node stream_stdout.js
```

## Publishing Packages

When you create your own package, and&nbsp;decide you want to publish your package to npm, it's as simple as running&nbsp; **npm publish** , and your code will be available for the world to enjoy!

```auto
npm publish
```


## Featured Products

### Raspberry Pi 1 Model B+ Starter Pack - Includes a Raspberry Pi 1

[Raspberry Pi 1 Model B+ Starter Pack - Includes a Raspberry Pi 1](https://www.adafruit.com/product/2125)
You're going to work hard with your Raspberry Pi B+. &nbsp;You're going to have to solder, code, and Linux your Maker heart out. &nbsp;That's why we've tried to make it as easy as possible to start -&nbsp;by combining the Raspberry Pi B+ with what we think are the best...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2125)
[Related Guides to the Product](https://learn.adafruit.com/products/2125/guides)
### Raspberry Pi 2 or Model B+ Starter Pack (Without Raspberry Pi)

[Raspberry Pi 2 or Model B+ Starter Pack (Without Raspberry Pi)](https://www.adafruit.com/product/2126)
You're going to work hard with your Raspberry Pi 2 Model B or Raspberry Pi 1 Model B+. &nbsp;You're going to have to solder, code, and Linux your Maker heart out. &nbsp;That's why we've tried to make it as easy as possible to start -&nbsp;by combining the Raspberry Pi with what...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/2126)
[Related Guides to the Product](https://learn.adafruit.com/products/2126/guides)
### Raspberry Pi 1 Model B Starter Pack - Includes a Raspberry Pi

[Raspberry Pi 1 Model B Starter Pack - Includes a Raspberry Pi](https://www.adafruit.com/product/1014)
You want to get hacking with your Pi fast, right? Get everything you need to start with the Adafruit Starter Pack for Raspberry Pi. It's the perfect accompaniment to your new Pi, everything you need to boot up your Pi Model B and get going. **We pre-assemble the Cobbler for you, no...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1014)
[Related Guides to the Product](https://learn.adafruit.com/products/1014/guides)
### Raspberry Pi Model B starter pack Doesn't include Raspberry Pi 1

[Raspberry Pi Model B starter pack Doesn't include Raspberry Pi 1](https://www.adafruit.com/product/955)
You want to get hacking with your Pi fast, right? Get everything you need to start with the Adafruit Starter Pack for Raspberry Pi. It's the perfect accompaniment to your new Pi, everything you need to get a distro image loaded and running. **We even pre-assemble the Cobbler for you,...**

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/955)
[Related Guides to the Product](https://learn.adafruit.com/products/955/guides)
### USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi

[USB to TTL Serial Cable - Debug / Console Cable for Raspberry Pi](https://www.adafruit.com/product/954)
The cable is easiest way ever to connect to your microcontroller/Raspberry Pi/WiFi router serial console port. Inside the big USB plug is a USB\<-\>Serial conversion chip and at the end of the 36" cable are four wire - red power, black ground, white RX into USB port, and green TX out...

In Stock
[Buy Now](https://www.adafruit.com/product/954)
[Related Guides to the Product](https://learn.adafruit.com/products/954/guides)
### Ethernet Cable - 10 ft long

[Ethernet Cable - 10 ft long](https://www.adafruit.com/product/730)
We have so many Internet-connected goodies in the shop, we figured it's time to carry a cable so you can easily connect them up! This cable is 10 feet long, black and has all 8 wires installed. Perfect for use with the [BeagleBone](http://www.adafruit.com/products/513), <a...></a...>

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/730)
[Related Guides to the Product](https://learn.adafruit.com/products/730/guides)
### Espruino 1.4 - Open Source Javascript Microcontroller

[Espruino 1.4 - Open Source Javascript Microcontroller](https://www.adafruit.com/product/1887)
Try the JavaScript of things with the **Espruino** - the world's first open-source JavaScript microcontroller! This little board has an STM32 microcontroller pre-programmed with Espruino all ready to go so you can start playing with Javascript-microcontrollers....

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1887)
[Related Guides to the Product](https://learn.adafruit.com/products/1887/guides)
### Assembled Pi T-Cobbler Plus - GPIO Breakout

[Assembled Pi T-Cobbler Plus - GPIO Breakout](https://www.adafruit.com/product/2028)
 **This is the assembled version of the Pi T-Cobbler Plus. &nbsp;It only works with the Raspberry Pi Model Zero, A+, B+, Pi 2, Pi 3, Pi 4, and Pi 5!** (Any Pi with 2x20 connector)  
  
The Raspberry Pi has landed on the Maker World like a 40-GPIO pinned, quad-USB ported,...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2028)
[Related Guides to the Product](https://learn.adafruit.com/products/2028/guides)

## Related Guides

- [Raspberry Pi E-mail Notifier Using LEDs](https://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds.md)
- [Adafruit's Raspberry Pi Lesson 4. GPIO Setup](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup.md)
- [Adafruit's Raspberry Pi Lesson 12. Sensing Movement](https://learn.adafruit.com/adafruits-raspberry-pi-lesson-12-sensing-movement.md)
- [Capacitive Touch Sensors on the Raspberry Pi](https://learn.adafruit.com/capacitive-touch-sensors-on-the-raspberry-pi.md)
- [Single Channel LoRaWAN Gateway for Raspberry Pi](https://learn.adafruit.com/raspberry-pi-single-channel-lorawan-gateway.md)
- [IoT Temperature Logger with Analog Devices ADT7410, Raspberry Pi, and Adafruit IO](https://learn.adafruit.com/iot-temperature-logger-with-python-and-adafruit-io.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
- [Analog Inputs for Raspberry Pi Using the MCP3008](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.md)
- [CircuitPython Libraries on Linux and Google Coral](https://learn.adafruit.com/circuitpython-on-google-coral-linux-blinka.md)
- [Basic Resistor Sensor Reading on Raspberry Pi](https://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-pi.md)
- [Adding Basic Audio Ouput to Raspberry Pi Zero](https://learn.adafruit.com/adding-basic-audio-ouput-to-raspberry-pi-zero.md)
- [Adafruit AMG8833 8x8 Thermal Camera Sensor](https://learn.adafruit.com/adafruit-amg8833-8x8-thermal-camera-sensor.md)
- [Windows IoT Core Application Development: Headed Blinky](https://learn.adafruit.com/windows-iot-application-development-headed-blinky.md)
- [LoRa and LoRaWAN Radio for Raspberry Pi](https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi.md)
- [Playing sounds and using buttons with Raspberry Pi](https://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-pi.md)
