Wrapping Things Up

As I described earlier, npm is one of the best things about developing in a node.js environment. Because we used a few third party dependencies in the examples, let's take a look at how you would wrap up all of the dependencies and include them in your project by creating a package.json file.


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

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

mkdir ~/pi_streams_example && cd ~/pi_streams_example

Now, we can use npm init to create our package.json file.

[email protected] ~/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 <[email protected]>
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": [
  "author": "Todd Treece <[email protected]>",
  "license": "ISC"

Is this ok? (yes) yes


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.

npm install --save onoff gpio-stream

By using npm install with the --save flag, npm will automatically install & save the dependencies to the package.json file that we just created.  All of your dependencies will now be installed in a folder called 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 has to do is run 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.  I have created a git repository that contains examples from this guide. Run the following command to clone the repo to your Pi.

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

Now, to install the dependencies, all you have to do is run npm install.

npm install

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

node stream_stdout.js

Publishing Packages

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

npm publish
Last updated on 2015-07-16 at 04.48.03 PM Published on 2014-12-19 at 04.57.54 PM