Node-RED Base64 Converter Node
This package provides a Node-RED node for encoding and decoding messages using Base64 format. It allows developers to convert `msg.payload` to a Base64 string if the input is a binary buffer, or decode a Base64 string back into a binary buffer. As of its stable `1.0.0` release, it offers robust and straightforward functionality for common Base64 transformations within Node-RED flows. Being part of the official Node-RED nodes collection, it often aligns with Node-RED core updates and feature milestones. Its primary differentiator is its seamless integration into the visual flow programming paradigm of Node-RED, handling the technical details of buffer and string conversions automatically based on input type. The package is maintained, indicating stability with less frequent, but impactful, updates. Its purpose is to simplify Base64 operations for Node-RED users without requiring custom function nodes.
Common errors
-
Error: Can't find node: node-red-node-base64 (or similar 'module not found' messages in Node-RED startup logs)
cause The node package was installed in an incorrect directory or Node-RED was not restarted after installation, preventing the runtime from discovering it.fixNavigate to your Node-RED user directory (e.g., `cd ~/.node-red`) and run `npm i node-red-node-base64`. Then, restart your Node-RED instance to reload the palette. -
TypeError: msg.payload.toString is not a function (or similar errors when handling the output of the base64 node)
cause The output type from the Base64 node (Buffer or string) was not what a subsequent node expected. For example, trying to call string methods on a Buffer, or Buffer methods on a string.fixVerify the input type to the Base64 node and, consequently, the expected output type. The node converts `Buffer -> string` and `string -> Buffer`. Adjust subsequent nodes (e.g., `debug` node configuration, `function` node logic) to correctly handle the actual output type.
Warnings
- gotcha This package is exclusively a Node-RED node and is not designed for direct programmatic import or use in standard JavaScript/TypeScript applications. Its functionality is exposed solely within the Node-RED runtime and editor.
- gotcha Node-RED nodes must be installed specifically within the Node-RED user directory (typically `~/.node-red`) for the runtime to discover and load them. Installing the package globally or as a regular project dependency will not make it available in the Node-RED palette.
- gotcha The Base64 node automatically determines whether to encode or decode based on the input `msg.payload`'s type. If the payload is a `Buffer`, it encodes to a string. If it's a Base64-encoded string, it decodes to a `Buffer`. Supplying other types may result in no action or unexpected behavior.
Install
-
npm install node-red-node-base64 -
yarn add node-red-node-base64 -
pnpm add node-red-node-base64
Imports
- base64 node type
import { base64 } from 'node-red-node-base64';{"id": "my-base64-node", "type": "base64", "name": "My Base64 Converter", "wires": [...]} (within Node-RED flow JSON) - Node-RED node installation
npm i node-red-node-base64 (run in a standalone project directory or globally)
npm i node-red-node-base64 (run within Node-RED user directory, e.g., ~/.node-red)
- Base64 encoding/decoding functionality
const encoded = Buffer.from('data').toString('base64'); // Direct Node.js API call, not using the node's functionality(Configure the 'base64' node in a flow; it automatically detects input type (Buffer for encode, string for decode) from msg.payload)
Quickstart
// 1. Ensure Node-RED is installed and running.
// 2. Install the node in your Node-RED user directory (typically ~/.node-red).
// Open your terminal and run:
// cd ~/.node-red
// npm i node-red-node-base64
// 3. Restart Node-RED if it was running.
// 4. Open your Node-RED editor (usually at http://localhost:1880).
// 5. Import the following JSON flow via Menu -> Import -> Clipboard:
[
{"id":"d2ccae00.2d335","type":"inject","name":"Inject Buffer","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":false,"x":136,"y":99,"z":"385bdf8b.c7a42","wires":[["e03cae10.1fc35"]]},{"id":"b778ef09.48871","type":"base64","name":"Encode to Base64","x":411.5,"y":160,"z":"385bdf8b.c7a42","wires":[["6295d1b1.9d6a3","46b597ba.b94a68"]]},{"id":"6295d1b1.9d6a3","type":"debug","name":"Encoded Output (String)","active":true,"console":"false","complete":"payload","x":610,"y":160,"z":"385bdf8b.c7a42","wires":[]},{"id":"ead9e7c9.152618","type":"debug","name":"Decoded Output (Buffer)","active":true,"console":"false","complete":"payload","x":610,"y":240,"z":"385bdf8b.c7a42","wires":[]},{"id":"46b597ba.b94a68","type":"base64","name":"Decode from Base64","x":411.5,"y":240,"z":"385bdf8b.c7a42","wires":[["ead9e7c9.152618"]]},{"id":"e03cae10.1fc35","type":"function","name":"Create Buffer Payload","func":"msg.payload = Buffer.from("Hello, Node-RED Base64!");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":160,"z":"385bdf8b.c7a42","wires":[["b778ef09.48871"]]}][
// ... (rest of the flow JSON as provided in the README, adjusted for clarity)
]