# "Issue" in RED.util.cloneMessage?

**URL:** <https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722>\
**Category:** General\
**Created:** [5 April 2026 21:24 UTC](https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722 "2026-04-05T21:24:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [5 April 2026 21:24 UTC](https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722/1 "2026-04-05T21:24:40Z")

</div>

I have the (strong) impression there's an "issue" in `RED.util.cloneMessage`. I've intentionally put those brackets around the word _issue_ as most users won't get in touch with it ever, yet ...

**Observation:**  
`RED.util.cloneMessage` wrongly changes weak arrays when cloning a message. In detail, weak ( == empty) array elements receive the value `undefined` during clone operation.

**Analysis:**  
`RED.util.cloneMessage` forwards the message object to `_.cloneDeep` (lodash) to perform the clone operation. Surprisingly, there's evidence that `_.cloneDeep` wrongly applies `undefined` to empty elements:

```auto
import * as _ from 'lodash';

// create a weak array, just fill index 3:
let x = new Array(5);
x[3] = 42;

console.log("Original array:")
x.forEach( (xx, i) => console.log(`${i}: ${xx}`));
console.log("");

console.log("Array after _.cloneDeep:")
let y = _.cloneDeep(x);
y.forEach( (xx, i) => console.log(`${i}: ${xx}`));
console.log("");

console.log("Array after structuredClone:")
let z = structuredClone(x);
z.forEach( (xx, i) => console.log(`${i}: ${xx}`));

```

output:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/2/92acc0cee1829ad3381b3b0841c5ab34b01c792c.png)

There was a [pull request](https://github.com/lodash/lodash/pull/5675) (in 2023) to address this issue - yet, despite merged, it looks like those changes got lost due to whatever reason...

**Possible fix:**  
As you can see, `structuredClone` clones the array correctly. Most probably, it should be mature enough to act as drop-in replacement for `_.cloneDeep`. It's a one-liner fix...

> <https://github.com/node-red/node-red/blob/b4eadc5c016fa8e7ecc067003d70202208bd10e1/packages/node_modules/%40node-red/util/lib/util.js#L105-L107>

Any comments?

---

<div class="post-metadata">

**Author:** ![omrid](https://avatars.discourse-cdn.com/v4/letter/o/77aa72/32.png) [@omrid](https://discourse.nodered.org/u/omrid)\
**Post date:** [6 April 2026 14:47 UTC](https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722/2 "2026-04-06T14:47:21Z")

</div>

There are gaps & limitations in every JS cloning method, including structuredClone(), mostly around properties which need to be serialized and then de-serialized.  
You need to analyze the type of objects you wish to clone: deep or shallow, do they have function members? external bindings/context? is there a risk of circular references? etc.  
Then you can select the cloning method that suits you (..., assign, structuredClone, JSON stringify/parse etc.)

I built a node for dashboard 2, and encountered many issues with standard cloning methods, so had to write my own cloning function (recursive JS. Not a big deal).

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [6 April 2026 19:24 UTC](https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722/3 "2026-04-06T19:24:13Z")

</div>

> [@omrid](#):
>
> had to write my own cloning function (recursive JS. Not a big deal).

Writing a "customized" cloning function might not be a be deal - but here we're talking about a topic within a function called million times a day, deep in the core of Node-RED. I'd prefer it works flawlessly for JS standard features...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 July 2026 19:24 UTC](https://discourse.nodered.org/t/issue-in-red-util-clonemessage/100722/4 "2026-07-05T19:24:36Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
