# Why doesn't RED.nodes.createNode apply the config to this itself?

**URL:** https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962
**Category:** Developing Nodes
**Created:** [20 August 2021 15:56 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962 "2021-08-20T15:56:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [20 August 2021 15:56 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/1 "2021-08-20T15:56:13Z")

</div>

I guess this may be one for @knolleary when he is back from his hols but ...

When you do `RED.nodes.createNode(this, config)` for you node instance, why doesn't `createNode` apply `config` to `this` for you?

It seems a long-winded step to have to go and individually apply the config properties to `this` yourself.

* * *

I am looking at the best ways to decompose the node runtime definiton and I think I've come a long way now. With some simple tricks it is possible to get a much clearer and more robust structure. I will post my progress to another thread shortly though some of the thinking is here:

> [@Reclaiming memory from Nodes](https://discourse.nodered.org/t/reclaiming-memory-from-nodes/49554/21):
>
> If you are interested in where I've got to in deconstructing the uibuilder runtime, please check out the v4.2.0-dev branch. It is quite interesting but I'm not sure I would recommend it except for complex nodes as it does need some advanced features of node.js. However, I have managed to eliminate the node variable completely. The exported function is only a few lines long now as all the work is done in separate functions. This has also resulted in some simplification of the code and the removal…

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 August 2021 16:14 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/2 "2021-08-20T16:14:24Z")

</div>

> [@TotallyInformation](#):
>
> why doesn't `createNode` apply `config` to `this` for you?

Because it didn't occur to us (me) to do something like that until it was too late to consider without potentially breaking nodes that didn't expect that behaviour.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [20 August 2021 16:19 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/3 "2021-08-20T16:19:45Z")

</div>

OK, thanks for that. It isn't a big deal - however, maybe something that could be improved (in a backwards compatible way of course) in a future release?

I'm also finding it facinating how that function treats `this` in a way that hadn't occured to me could happen.

Basically, when using a function outside of the export. If you try to access `this` before the call to `createNode`, you can not. But if you try afterwards, you can! Very strange - though I'm sure there will be an answer I'm too thick to have comprehended 😀

Just writing up a minimal example node definition using what I've discovered so far about destructuring the code.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 August 2021 16:20 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/4 "2021-08-20T16:20:55Z")

</div>

> [@TotallyInformation](#):
>
> big deal - however, maybe something that could be improved (in a backwards compatible way of course) in a future release?

That was my point... there is no way to introduce it in a backward compatible way.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [20 August 2021 16:25 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/5 "2021-08-20T16:25:12Z")

</div>

If the config properties were added to `this` in `createNode`, I think that _would_ be compatible?

For all existing nodes that use the `this = that` and then add the config properties manually, that would still work just fine I think.

But new nodes could miss off that step if they wanted to.

There are some overlapping property names but they all seem to have the same content anyway so I don't think that matters either?

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 August 2021 16:41 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/6 "2021-08-20T16:41:57Z")

</div>

> [@TotallyInformation](#):
>
> There are some overlapping property names but they all seem to have the same content anyway so I don't think that matters either?

But you cannot guarantee it. A node may have some logic around setting `this.foo` based on the value of `config.foo` and other properties. If we unilaterally set it, it would break. I remember finding a concrete examples of this when I looked at it a few years ago.

> [@TotallyInformation](#):
>
> If you try to access `this` before the call to `createNode` ,

The node object, with all of its internal workings, hasn't been created until you call `createNode`. That is why it's the first thing the node constructor function needs to do.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [20 August 2021 16:48 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/7 "2021-08-20T16:48:26Z")

</div>

> [@knolleary](#):
>
> But you cannot guarantee it

Thanks for the explanation Nick, that makes sense.

> [@knolleary](#):
>
> hasn't been created until you call `createNode`

It is just weird that you HAVE to pass `this` (I tried it with something else 😀 ) even though you cannot actually reference `this` in your code until after the call.

I'm sure there is a logical reason for it, it just doesn't _look_ logical. However, it works and I'm using it. So not worth disturbing anyone about.

Forcing me to learn more about the inner workings of Node-RED! 🕶

---

<div class="post-metadata">

### Author: ![kevinGodell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kevingodell/32/27040_2.png) [@kevinGodell](https://discourse.nodered.org/u/kevinGodell)
#### Post date: [20 August 2021 20:49 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/8 "2021-08-20T20:49:49Z")

</div>

Currently, I use createNode as:

```auto
class MyCustomNode {
  constructor(config) {
    createNode(this, config);
  }
}

```

It would be awesome if it could be:

```auto
class MyCustomNode extends CreateNode {
  constructor(config) {
    super(config);
  }
}

```

EDIT: I just ran a quick and dirty test to see what [createNode](https://github.com/node-red/node-red/blob/master/packages/node_modules/%40node-red/runtime/lib/nodes/index.js#L93) does. It seems that the main thing it does is to attach any credentials to the Node object.

I was able to extend the Node class to create a functional node-red node without the need to call createNode like this:

```auto
const Node = require('/usr/lib/node_modules/node-red/node_modules/@node-red/runtime/lib/nodes/Node.js');

class Mp4fragNode extends Node {
    constructor(config) {
      super(config);
  }
}

```

It simply worked (probably because my node does not use credentials). If the credential grabbing logic was moved from createNode to the Node class constructor, then createNode seems like it could remain backwards compatible, while also allowing the Node class to be exportable to be used as shown above with the extends class syntax.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 August 2021 21:41 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/9 "2021-08-20T21:41:21Z")

</div>

There have been some nodes implemented using 'true' JavaScript classes.

You're right they are largely compatible and possibly a clearer way to structure things. If we were to change our recommended approach, I suspect this would be more akin to what we'd do.

---

<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: [19 October 2021 21:41 UTC](https://discourse.nodered.org/t/why-doesnt-red-nodes-createnode-apply-the-config-to-this-itself/49962/10 "2021-10-19T21:41:26Z")

</div>

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