IMD SolutionComputing LibraryShop main store

Troubleshooting

Node-RED crash on Azure IoT Hub: DeviceId=undefined credentials fix

Edge computing and IIoT Published 6 min read By IMD Solution engineering team

Short answer

The crash is two faults: the hub refuses the request because the node builds DeviceId=undefined and SharedAccessKey=undefined from wrong property names in your Inject payload, and the resulting uncaught TypeError then exits the runtime with status=1/FAILURE. Rename the payload fields to deviceId, key, protocol and data, or put the full connection string in the node, and the restart loop ends.

A laptop showing the error log of a flow editor on an edge PC, a node failing on a full disk and a permission error on a serial port, cabled to a black fanless edge PC on the bench
On this page
  1. Two faults in one log
  2. Check the checkpoints in order
  3. Why the credentials read as undefined
  4. Connection string or message credentials
  5. Correct the message and prove it
  6. Stop the restart loop and verify the session
  7. The decision
  8. Questions asked

Your runtime is dying on two separate faults, and the credentials come first. The log line HostName=learning-hub.azure-devices.net;DeviceId=undefined;SharedAccessKey=undefined shows the hub address reaching the node while both device credentials come out as undefined, so the hub answers Connection refused: Not authorized. The TypeError: Cannot read property 'on' of undefined that follows is the node's client-construction path failing on the rejected connection, and that uncaught exception is what exits the process with status=1/FAILURE. Fix the property names your Inject node sends before touching the network, the hostname or the hub; the crash stops when authentication succeeds.

Two faults in one log #

Read the failure as a chain of three events, not one:

  • Rejected authentication. The node assembled a connection with no device identity and no key, and the hub refused it as Not authorized. This is the root cause.
  • Uncaught TypeError. The node's error-handling path then dereferenced a client object that was never created, Node-RED logged an uncaught exception and the process exited. This is a consequence.
  • A misleading status line. The [info] message Connected to Azure IoT Hub printed after the refusal is no proof of a session; only a test message arriving at the hub is.

For the record, the failing installation that produced this log ran Node-RED v0.18.4 on Node.js v8.11.1, Linux 4.14.34-v7+ arm LE, with a node of type azureiothub. Those readings describe one failing installation, not a known affected-version range; the fault is in the data the node received, not in the runtime version.

Check the checkpoints in order #

Work from the wire inward so you change one thing at a time:

  • Link and IP. A down interface or missing address stops the request before authentication. If ip a shows no usable address, restore that first.
  • Hostname resolution. The request must resolve the hub hostname. In this log the hub itself answered, so resolution and routing were already working; do not spend time on DNS for this failure.
  • Authentication. Not authorized means the request arrived intact and the hub refused the credentials. That moves the investigation to the values the node assembled, which is exactly what the connection-string log line shows you.
  • Client construction. The TypeError sits downstream of the refusal; it will disappear once the node builds a working client.

Why the credentials read as undefined #

In JavaScript, reading a property that does not exist returns undefined, and property names are case-sensitive. The Inject payload in the failing flow carried four properties; three of them misspelled relative to what the installed node reads, and both credential fields ended up unread: 2 required credentials supplied under the wrong names, 0 resolved, so the connection string came out as DeviceId=undefined;SharedAccessKey=undefined.

Property your Inject sends Property the node reads Why it fails
deviceID deviceId The final letter differs in case; these are distinct names
SAK key A different name; an older revision of the node read SAK instead
Protocol protocol Capital first letter
Data data Capital first letter; the node also expects a string here, not an object

The documented contract of the Send Payload node in this package family is explicit: the JSON in the payload field must contain deviceId, key, protocol and data. Supplying a credential somewhere in the flow is not the same as supplying it under the name and at the location the installed node reads. Do not add both spellings to be safe; a duplicate field hides the mismatch instead of resolving it.

Connection string or message credentials #

Two configurations circulate for this node family, and they consume credentials in different places. Open the node's edit dialog and let what you see decide the path:

Setting Connection-string path Message-credential path
Hub address HostName inside the connection string Hostname field in the node dialog
Device identity DeviceId inside the connection string deviceId in the incoming message
Device credential SharedAccessKey inside the connection string key in the incoming message
Injected content A plain string such as Hello from device An object with credentials, protocol and data
Transport Node configuration protocol in the message

If the dialog has a connection-string field, put the complete device connection string there, with hostname, device identity and key that all belong to the same device entry, and keep it in the node's credential configuration rather than in Debug output. If the dialog has only a hostname field, the credentials must arrive in the payload, and the field-name fix below is your path. Either way the device identity must already exist in the hub's device registry under exactly that ID.

Correct the message and prove it #

For the message-credential path, the payload the node accepts looks like this example; the key value shown is an illustrative string that will not authenticate, so replace it with the key from your own device entry:

{
  "deviceId": "testenode1",
  "key": "ZXhhbXBsZWtleTAxMjM0NTY3ODk=",
  "protocol": "amqp",
  "data": "{tem: 25, wind: 20}"
}

Two details matter beyond the names. Set the Inject node's payload type to a parsed JSON object, because a string containing JSON text exposes no properties at all. And note that data is a string in this contract, so an object such as { "Light": "25" } in that slot is another mismatch to fix.

Prove the correction before wiring the Azure node back in. Place this diagnostic Function node immediately upstream:

// Diagnostic Function node: placed before the azureiothub node
let p = msg.payload; // expected: a parsed object, not a string
node.warn("deviceId is " + typeof p.deviceId);
node.warn("key is " + typeof p.key);
node.warn("protocol is " + typeof p.protocol);
node.warn("data is " + typeof p.data);
return msg;

The test that proves the result: with the old payload, the sidebar warnings read deviceId is undefined and key is undefined; with the corrected payload, all four warnings read string. Redact the key before sharing that output. One more variable: the failing flow requested mqtt while the corrected examples use amqp. Change the credentials first and the transport second, so you know which change mattered. If you keep MQTT, the hub serves it over TLS on port 8883 and refuses plain why Azure IoT Hub MQTT connections fail on port 1883, so the firewall must allow that outbound port.

Stop the restart loop and verify the session #

While the flow still faults, each automatic restart reloads the same broken message. Disconnect the wire from the Inject node to the Azure node, or set the Inject to manual trigger only, until the diagnostic passes. If repeated crashes have left the runtime unstable, recover it with the safe-mode procedure in Node-RED crash after installing nodes: safe mode and rollback, then redeploy the corrected flow.

Verification happens on the hub side, not in the Node-RED log: inject one test value with a known timestamp and confirm it appears in the hub's telemetry monitoring or at a downstream consumer, as covered in Troubleshooting Azure IoT Hub Sensor Data Visibility. Only that receipt proves a session.

The decision #

  • Log shows DeviceId=undefined or SharedAccessKey=undefined: rename the payload properties to deviceId, key, protocol and data, with a parsed-object payload type. This is the fix for this failure.
  • Node dialog has a hostname field only: stay on the message-credential path and send the object above.
  • Node dialog has a connection-string field: put the full device connection string in the node and send plain telemetry payloads.
  • Not authorized persists with correct field names: the device identity is missing from the hub registry or the key does not match that entry; re-copy both from the device's registry page.
  • TypeError gone, message received: done. If a TypeError ever recurs with a working session, capture the full stack and record it with the installed package version before changing dependencies.

Questions asked

Does the Connected to Azure IoT Hub log line prove the session works?

No. In this failure the node prints that line right after the refusal, so it is a status message from the node, not confirmation from the hub. Confirm on the hub side: inject a test value with a known timestamp and watch for it in the hub's device-to-cloud telemetry monitoring or at a downstream consumer within a few seconds.

Where does the device key value come from?

From the device's entry in the hub's device registry. Each registered device holds two symmetric keys, primary and secondary; copy one exactly, with no trailing space or newline, and make sure the deviceId in your payload matches that registry entry character for character. A key from another device or a deleted entry authenticates nothing.

Can I keep my existing Inject payload and rename the fields in a Function node instead?

Yes. In a Function node placed before the Azure node, read the old properties and write them under the names the node reads: take the deviceID property and assign it to deviceId, the SAK property to key, Protocol to protocol and Data to data, then return the message. Rename once in one place; do not add both spellings, because a duplicate field hides which one the node actually consumed.

What if my log shows a different error than Not authorized?

A name-resolution failure before authentication points at DNS or a misspelled hub hostname. A connection timeout with no refusal points at the firewall: the device must reach the hub outbound on port 8883 for MQTT over TLS or 5671 for AMQP over TLS. Fix that path first, because no credential change matters while the request cannot arrive.

Keep reading

  • How-to

    Dynamically create IoT Central devices from Node-RED

    A Node-RED device connector authenticates and publishes telemetry; it does not create device identities. Register one device manually, prove the connection end to end, then automate enrollment through Azure Device Provisioning Service or pre-registered device IDs with SAS keys, one identity per sensor.

    Edge computing and IIoT

  • Troubleshooting

    Why an uncaught ECONNRESET stops Node-RED when a PLC drops

    Node-RED stops because the TCP reset reaches it as an uncaught exception: the S7 node or its transport dependency never caught the reset and left the connection state undefined, so the only safe runtime response is to exit. The fix belongs in that node or dependency, and neither Catch nodes nor runtime settings can intercept it. Verify by holding the PLC offline past your observed failure interval, restoring it, and requiring zero Uncaught Exception lines with the endpoint reconnected.

    Edge computing and IIoT

  • Troubleshooting

    Node-RED will not start after an npm update on an IOT2040

    On a gateway still running Node.js v6.14.3, a blanket npm update installs a Node-RED release that needs Node.js 8 or newer, so the autostart entry loads nothing. Confirm the runtime with node -v, then reinstall the last compatible release, Node-RED 0.19.5, using npm's global install pinned to that version. Fix npm download failures first; a re-image removes the mismatch but not the runtime ceiling.

    Edge computing and IIoT

Why oversized history blocks spike historian RAM at merge