Skip to main content

file-read

Read file contents from the local filesystem in various formats.

Overview

The file-read node reads the contents of a file from the local filesystem. It supports multiple output formats including plain text, binary buffers, parsed JSON, and line-by-line streaming. The filename can be set statically in the node configuration or dynamically via msg.filename.

Text
UTF-8 string
Binary
Raw Buffer
JSON
Parsed object
Lines
One msg per line

Properties

Property Type Default Description
filename string "" Path to the file. Can be overridden by msg.filename.
format string "text" Output format: text, binary, json, or lines
encoding string "utf-8" Character encoding: utf-8, ascii, or base64

Output Formats

text

Returns the file content as a single string in msg.payload. Best for configuration files, templates, and small text files.

binary

Returns the raw file content as a Node.js Buffer. Use for images, firmware binaries, or compressed archives.

json

Reads the file and parses it as JSON. Returns a JavaScript object in msg.payload. Errors if the file is not valid JSON.

lines

Reads the file line by line, emitting one message per line. The msg.parts property tracks sequence for later reassembly.

Example: Read CSV Sensor Log

Read a CSV file containing temperature sensor readings and process each line. Uses the lines format to stream large files without loading everything into memory.

// file-read node configuration
{
  "filename": "/data/sensors/temperature-log.csv",
  "format": "lines",
  "encoding": "utf-8"
}

// CSV file contents:
// timestamp,sensor_id,temperature,humidity
// 2024-01-15T10:00:00Z,dht22-01,23.5,65
// 2024-01-15T10:05:00Z,dht22-01,23.7,64

// Each line emitted as a separate msg:
{
  "payload": "2024-01-15T10:00:00Z,dht22-01,23.5,65",
  "parts": {
    "id": "read001",
    "index": 1,
    "count": 3,
    "type": "string",
    "ch": "\n"
  },
  "filename": "/data/sensors/temperature-log.csv"
}

Example: Load JSON Configuration

Load a JSON configuration file and store it in flow context so other nodes can access the settings. Pair with file-watch for automatic reload on changes.

// file-read node configuration
{
  "filename": "/etc/edgeflow/sensors.json",
  "format": "json",
  "encoding": "utf-8"
}

// JSON file contents:
// {
//   "sensors": [
//     { "id": "dht22-01", "pin": 4, "interval": 5000 },
//     { "id": "bme280-01", "bus": 1, "interval": 10000 }
//   ],
//   "mqtt_topic_prefix": "home/sensors"
// }

// Output msg.payload (auto-parsed):
{
  "sensors": [
    { "id": "dht22-01", "pin": 4, "interval": 5000 },
    { "id": "bme280-01", "bus": 1, "interval": 10000 }
  ],
  "mqtt_topic_prefix": "home/sensors"
}

// In a function node downstream:
// flow.set('config', msg.payload);
// node.status({ text: msg.payload.sensors.length + " sensors" });

Example: Read Firmware Binary for OTA

Read a firmware binary file and prepare it for over-the-air updates to connected devices.

// file-read node configuration
{
  "filename": "/data/firmware/esp32-v2.1.bin",
  "format": "binary",
  "encoding": "utf-8"
}

// Output msg:
{
  "payload": "",
  "filename": "/data/firmware/esp32-v2.1.bin",
  "_msgid": "fr003"
}

// msg.payload is a Buffer containing raw bytes
// msg.payload.length gives the file size in bytes

Common Use Cases

Load Configuration

Read JSON or YAML config files at startup or on change

Process Data Files

Read CSV, JSON, or text data files for batch processing

Serve Static Content

Read HTML templates or static assets for HTTP responses

Firmware Distribution

Read binary firmware files for OTA device updates

Related Nodes