# Migrating configuration to a configuration node (or, programmatically instantiating a config node)

**URL:** <https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154>\
**Category:** Developing Nodes\
**Created:** [12 April 2022 08:15 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154 "2022-04-12T08:15:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sverre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sverre/32/54040_2.png) [@sverre](https://discourse.nodered.org/u/sverre)\
**Post date:** [12 April 2022 08:15 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154/1 "2022-04-12T08:15:34Z")

</div>

I'm considering moving some configuration fields from an existing node to a (new) config node, but would like to avoid a breaking change if possible. Is there a way to instantiate a config node programmatically? If there is, I figured I could just hide the original input fields from the edit node dialog, check if a config node instance matching any "legacy" config exists, and instantiate it if it doesn't. (I would also remove the legacy config, so the check wouldn't have to run again.)

I'm guessing it's is not possible, as the only thing I found on the topic was someone who talked about it being a breaking change. Figured I should ask, though, things might have changed in the ~ year since.

---

<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:** [11 June 2022 08:16 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154/2 "2022-06-11T08:16:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [3 August 2022 15:29 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154/3 "2022-08-03T15:29:38Z")

</div>



---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [3 August 2022 20:00 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154/4 "2022-08-03T20:00:27Z")

</div>

Hi @sverre,

I had overlooked your question, due to the large amount of posts that are created nowadays on Discourse. But it was a very good question!

Last night I was also wondering how I needed to do it, so I found your post. I have asked Paul to reopen this discussion, so I can share my solution. Perhaps you don't need it anymore, but then others might benefit from it ...

## The original node

In your original node you have a property with a default value:

```auto
<script type="text/javascript">
    RED.nodes.registerType('my-node',{
        defaults: {
            originalProperty: {value: "defaultValue"}
        },    
        ...
    });
</script>

```

That property is displayed on the node's config screen:

```auto
<script type="text/x-red" data-template-name="my-node">
   ...
   <div class="form-row">
        <label for="node-input-originalProperty">Original property</span></label>
        <input type="number" id="node-input-originalProperty">
   </div>
   ...
</script>

```

And the entered property value becomes available on the server side after a deploy:

```auto
 module.exports = function(RED) {
    var settings = RED.settings;

    function MyNode(config) {
        RED.nodes.createNode(this, config);
        this.originalProperty = config.originalProperty;
        ...
    }
    
    RED.nodes.registerType("my-node", MyNode);
}

```

So far so good. But as soon as lots of these nodes are added to a flow, people need to start copying the value of originalProperty to all those nodes to keep them in sync. Which is rather annoying...

## The updated node

So you want to introduce a config node, so users can enter their value in that config node (and re-use it in multiple nodes). But you need to migrate the original value into the config node, otherwise people will loose their original property values (because the original input field won't be visible anymore).

1. [Create](https://nodered.org/docs/creating-nodes/config-nodes) a config node (i.e. html and js file) which contains now the originalProperty:

2. Remove the original html input element _"node-input-originalProperty"_ because the original property shouldn't be visualized anymore on the node's config screen. Instead we will add a new input field, that will be used as a placeholder where Node-RED can show a dropdown with all available config nodes:

3. Since the original property values need to be migrated, make sure that those values are still being remembered by Node-Red by keeping the originalProperty under the cover:

4. As soon as the config screen of our original node is opened, a new config node needs to be instantiated (when there is no config node yet and there are original values to be migrated):

5. Update the server side (js file) to use the originalProperty from the config node. Or if there is no config node yet, use the originalProperty which is still available in the node (which will be the case if the user has not opened the node's config screen yet):

This way a new config node is automatically created when you open the node's config screen, where the original property value is displayed. And when a new node is dragged onto the flow, no new config node is created automatically: indeed the user perhaps want to re-use an existing config node...

Good luck!  
Bart

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [3 August 2022 20:01 UTC](https://discourse.nodered.org/t/migrating-configuration-to-a-configuration-node-or-programmatically-instantiating-a-config-node/61154/5 "2022-08-03T20:01:37Z")

</div>

P.S. Perhaps my code doesn't cover all edge cases, because I wrote it last night and so it hasn't been fully tested yet. If I find any issue, I will report it here...
