To begin, create a directory called nodered to house your Node-RED-based code. Then open a terminal to this directory and create an initial package.json.
| | $ npm init -y |
Next, install the node-red package locally.
| | $ npm install --save --save-exact node-red@0.16.2 |
Then open your package.json in a text editor and add a start script to the scripts section like so:
| | "scripts": { |
| » | "start": "node-red -v -u ./config -f ./config/flows.json", |
| | "test": "echo \"Error: no test specified\" && exit 1" |
| | }, |
Here we’re specifying a couple of command-line arguments to the node-red command. The -v flag turns on verbose mode. The -u flag is short for --userDir and specifies a path to a directory to store configuration information.
The -f flag points to a JSON file that will contain our flows. In Node-RED, a flow is a program that directs events as they flow through a network of nodes.
You’ll be working with flows a lot in this chapter. To begin, run npm start now:
| | $ npm start |
| | |
| | > nodered@1.0.0 start ./code/nodered |
| | > node-red -v -u ./config -f ./config/flows.json |
| | |
| | 19 Jun 04:34:00 - [info] |
| | |
| | Welcome to Node-RED |
| | =================== |
| | |
| | 19 Jun 04:34:00 - [info] Node-RED version: v0.16.2 |
| | 19 Jun 04:34:00 - [info] Node.js version: v8.1.0 |
| | 19 Jun 04:34:00 - [info] Linux 4.4.0-79-generic x64 LE |
| | 19 Jun 04:34:00 - [info] Loading palette nodes |
| | 19 Jun 04:34:01 - [warn] ------------------------------------------------------ |
| | 19 Jun 04:34:01 - [warn] [rpi-gpio] Info : Ignoring Raspberry Pi specific node |
| | 19 Jun 04:34:01 - [warn] ------------------------------------------------------ |
| | 19 Jun 04:34:01 - [info] Settings file : ./code/nodered/config/settings.js |
| | 19 Jun 04:34:01 - [info] User directory : ./code/nodered/config |
| | 19 Jun 04:34:01 - [info] Flows file : ./code/nodered/config/flows.json |
| | 19 Jun 04:34:01 - [info] Server now running at http://127.0.0.1:1880/ |
| | 19 Jun 04:34:01 - [info] Starting flows |
| | 19 Jun 04:34:01 - [info] Started flows |
Toward the end of the output, it’ll inform you that the server is listening on port 1880. If you open http://localhost:1880 in a browser you should see something like this:

If you see this, great! Back in your terminal, use Ctrl-C to shut down the server. Next we’ll set up some security around Node-RED.