本地运行Node-RED

If you are on a Raspberry Pi or any Debian-based operating system, including Ubuntu and Diet-Pi, you can use the Pi install script available here.
If you are on an RPM-based operating system, including RedHat, Fedora and CentOS, you can use the RPM install script available here.
If you are using Windows, detailed instructions for installing Node-RED can be found here.

先决条件

要在本地安装Node-RED,您需要受支持的Node.js版本

使用npm安装

要安装Node-RED,您可以使用node.js自带的npm命令:

sudo npm install -g --unsafe-perm node-red

如果您使用的是Windows系统,请不要以sudo命令开头。

该命令将全局安装Node-RED及其依赖项。

如果命令输出的末尾看起来类似以下内容,则可以确认已成功:

+ node-red@1.1.0
added 332 packages from 341 contributors in 18.494s
found 0 vulnerabilities

使用Docker安装

要以最简单的方式在Docker中运行,只需执行:

docker run -it -p 1880:1880 --name mynodered nodered/node-red

如需更详细信息,请参阅我们的docker指南。

使用snap安装

如果您的操作系统支持 Snap,您可以通过以下方式安装 Node-RED:

sudo snap install node-red

当以Snap包形式安装时,它将在安全容器中运行,该容器无法访问您可能需要使用的一些额外功能,例如:

  • 访问主系统存储。只能读写本地主目录。
  • gcc - 用于编译您想安装节点的任何二进制组件
  • git - 如需使用项目功能则需安装
  • 直接访问GPIO硬件
  • 访问您的流程想要与Exec节点一起使用的任何外部命令(例如)。

如果您需要访问系统硬件或添加需要编译的节点,我们建议完整安装Node-RED而不要使用snap包。

运行中

安装为全局模块后,您可以使用node-red命令在终端中启动Node-RED。您可以使用Ctrl-C或关闭终端窗口来停止Node-RED。

$ node-red

Welcome to Node-RED
===================

30 Jun 23:43:39 - [info] Node-RED version: v1.3.5
30 Jun 23:43:39 - [info] Node.js  version: v14.7.2
30 Jun 23:43:39 - [info] Darwin 19.6.0 x64 LE
30 Jun 23:43:39 - [info] Loading palette nodes
30 Jun 23:43:44 - [warn] rpi-gpio : Raspberry Pi specific node set inactive
30 Jun 23:43:44 - [info] Settings file  : /Users/nol/.node-red/settings.js
30 Jun 23:43:44 - [info] HTTP Static    : /Users/nol/node-red/web
30 Jun 23:43:44 - [info] Context store  : 'default' [module=localfilesystem]
30 Jun 23:43:44 - [info] User directory : /Users/nol/.node-red
30 Jun 23:43:44 - [warn] Projects disabled : set editorTheme.projects.enabled=true to enable
30 Jun 23:43:44 - [info] Creating new flows file : flows_noltop.json
30 Jun 23:43:44 - [info] Starting flows
30 Jun 23:43:44 - [info] Started flows
30 Jun 23:43:44 - [info] Server now running at http://127.0.0.1:1880/red/

然后您可以通过在浏览器中访问 http://localhost:1880 来进入Node-RED编辑器。

日志输出为您提供各种信息片段:

  • Node-RED 和 Node.js 的版本
  • 尝试加载调色板节点时遇到的任何错误
  • 您的设置文件及用户目录的位置
  • 它正在使用的流文件名称。

Node-RED默认使用flows_.json作为流程文件。您可以通过将流程文件名作为参数传递给node-red command来更改此设置。

命令行用法

Node-RED可以通过命令node-red启动。该命令可以接受多种参数:

node-red [-v] [-?] [--settings settings.js] [--userDir DIR]
         [--port PORT] [--title TITLE] [--safe] [flows.json|projectName]
         [-D X=Y|@file]
选项 描述
-p, --port PORT Sets the TCP port the runtime listens on. Default: 1880
--safe Starts Node-RED without starting the flows. This allows you to open the flows in the editor and make changes without the flows running. When you deploy your changes, the flows are then started.
-s, --settings FILE Sets the settings file to use. Default: settings.js in userDir
--title TITLE Set process window title
-u, --userDir DIR Sets the user directory to use. Default: ~/.node-red
-v Enables verbose output
-D X=Y|@file Override individual settings
-?, --help Shows command-line usage help and exits
flows.json|projectName If the Projects feature is not enabled, this sets the flow file you want to work with. If the Projects feature is enabled, this identifies which project should be started.

Node-RED默认使用flows_.json作为流程配置文件。如果运行环境的主机名可能发生变化,您应当指定一个固定的文件名;可以通过命令行参数或在settings file中使用flowsFile选项进行设置。

覆盖个别设置

自 Node-RED 1.1.0 起

您可以使用命令行选项-D(或--define)来覆盖单个设置。

例如,要更改日志记录级别,您可以使用:

-D logging.console.level=trace

您也可以将自定义设置作为文件提供:

-D @./custom-settings.txt

该文件应包含要覆盖的设置列表:

logging.console.level=trace
logging.console.audit=true

向底层Node.js进程传递参数

在某些情况下,需要向底层的Node.js进程传递参数。例如,在运行于内存受限的设备上时,比如树莓派或BeagleBone Black。

要实现这一点,您必须使用node-red-pi启动脚本来替代node-red注意:该脚本在Windows系统上不可用。

另外,如果使用node命令运行Node-RED,必须在指定red.js和要传递给Node-RED本身的参数之前,为node进程提供参数。

以下两个命令展示了这两种方法:

node-red-pi --max-old-space-size=128 --userDir /home/user/node-red-data/
node --max-old-space-size=128 red.js --userDir /home/user/node-red-data/

升级Node-RED

If you installed Node-RED using the Pi script, you can rerun it to upgrade. The script is available here.

如果您已将Node-RED作为全局npm包安装,可以通过以下命令升级至最新版本:

sudo npm install -g --unsafe-perm node-red

如果您使用的是Windows系统,请不要以sudo命令开头。

下一步