Node-Red安裝套件

自從三、四個星期前在天瓏書局看到一本有關node-red的書後,才知道原來在RaspberryPI中也有這種IOT圖型化介面開發工具。當天回家後到node-red官網大約了解了一下,也發現可以用docker的方式來執行,就想說找個時間來試玩一下。

今天正好有點時間,於是就依node-red官網的docker使用方式啟動了一組node-red。一開始試用下來還算順利,可以完成一個最基本的訊息輸入(inject)及輸出到debug區的小程式。但接著想要將這個訊息用email方式傳送時就遇到困難了。

在node-red的docker image中是不含有其它額外套件的,而要用email來傳送訊息則是要再另外安裝套件。原本想說就只要用docker連上目前在運行的node-red image,再執行安裝指令就好了。沒想到一執行就出現以下的錯誤訊息:

npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.npmjs.org/node-red-node-email failed, reason: getaddrinfo ENOTFOUND registry.npmjs.org
npm ERR! network This is a problem related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network ‘proxy’ config is set properly. See: ‘npm help config’

npm ERR! A complete log of this run can be found in:
npm ERR! /usr/src/node-red/.npm/_logs/2021-11-07T05_39_50_589Z-debug.log

初步看來大約知道是無法存取到套件所在的網站。於是就先開始排除是否為網路的問題。執行了以下指令,發現在node-red的image中看來要執行DNS查找網站是有問題的。一開始也沒想到要重新再做一份image檔,於是就先求助google大神,看是否有其它人遇到相同的問題。

查看後,多數得到的答案就是要去清除npm的cache或是要指定npm的registry網址:

npm config set proxy null
npm config set proxy https://registry.npmjs.org

但不論如到設定,看來都無法成功。只好回頭去看看node-red網站中針對docker的說明。發現有個段落提package.json這個檔案的用途,它主要是用來指示node-red在建立docker image檔時要額外加入哪些套件。這樣看來目前唯一的解法就是自已建立一組新的node-red docker image了。

客制化node-red docker image

要客制化自己的node-red docker image,也不用就完全從頭開始。可以參考node-red的docker github網站,它有基本的說明,以及許多可用來讓你快速建置客制化node-red docker image的檔案。而且客制化的方式還可以讓你選你慣用的linux發行版本,目前有debian(ubuntu)及alphine兩種版本。我個人慣用ubuntu,所以打算用debian的方式來建置。

首先你可以先將node-red的docker相關資料用git clone一份到你的本機上。接著就是去修改docker-custom中的docker-debian.sh:

#!/bin/bash
export NODE_RED_VERSION=$(grep -oE "\"node-red\": \"(\w*.\w*.\w*.\w*.\w*.)" package.json | cut -d\" -f4)

echo "#########################################################################"
echo "node-red version: ${NODE_RED_VERSION}"
echo "#########################################################################"

docker build --rm --no-cache \
    --build-arg ARCH=arm64v8 \
    --build-arg NODE_VERSION=12 \
    --build-arg NODE_RED_VERSION=${NODE_RED_VERSION} \
    --build-arg OS=buster-slim \
    --build-arg BUILD_DATE="$(date +"%Y-%m-%dT%H:%M:%SZ")" \
    --build-arg TAG_SUFFIX=default \
    --file Dockerfile.debian \
    --tag proglab/node-red:1.0 .

在 docker-debian.sh 中,你需要改兩個地方:

  • 你要留意的是ARCH,因為預設是x86,但我的執行環境是在RaspberryPI中,所以我要改為ARCH=arm64v8。
  • 另外要留意的是 –tag,這個部份請設定為你想要的docker image 標籤。

接著就是要修改package.json,在這裏我主要是在dependencies區段中加入我要的套件node-red-node-email:

{
    "name": "node-red-docker",
    "version": "2.1.3",
    "description": "Low-code programming for event-driven applications",
    "homepage": "http://nodered.org",
    "license": "Apache-2.0",
    "repository": {
        "type": "git",
        "url": "https://github.com/node-red/node-red-docker.git"
    },
    "main": "node_modules/node-red/red/red.js",
    "scripts": {
        "start": "node $NODE_OPTIONS node_modules/node-red/red.js $FLOWS",
        "debug": "node --inspect=0.0.0.0:9229 $NODE_OPTIONS node_modules/node-red/red.js $FLOWS",
        "debug_brk": "node --inspect=0.0.0.0:9229 --inspect-brk $NODE_OPTIONS node_modules/node-red/red.js $FLOWS"
    },
    "contributors": [
        {
            "name": "Dave Conway-Jones"
        },
        {
            "name": "Nick O'Leary"
        },
        {
            "name": "James Thomas"
        },
        {
            "name": "Raymond Mouthaan"
        }
    ],
    "dependencies": {
        "node-red": "2.1.3",
        "node-red-node-email": "*"
    },
    "engines": {
        "node": ">=12"
    }
}

都調整完成後,接著就是執行docker-debian.sh。基本上只要等一下子,它就會自動產生新的一份node-red的docker image了。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *