# Dynamic node creation in the Node-RED editor

**URL:** <https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983>\
**Category:** General\
**Tags:** editor\
**Created:** [27 April 2023 13:22 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983 "2023-04-27T13:22:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 13:22 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/1 "2023-04-27T13:22:30Z")

</div>

Hi there,

I've read many comments pointing out that the Node-RED editor is not meant to be dynamic nor should it be doing any computation. I've gone and ignored that because I seem to have made an assumption that Node-RED is a [Yahoo Pipes](https://en.wikipedia.org/wiki/Yahoo!_Pipes) clone, it isn't of course.

I find the editor extremely useful as a mind map tool, Node-RED wasn't meant to be that either but it does a better job than most other mind map tools out there. Plus its easy to extend to make a mind map that actually fits to my mind! In addition to that, my mind map is executable so that I can chain nodes together and generate markdown files from the pathways between nodes. (That I do on the server side.)

For my mind-map, I created the [find-all-orphan-nodes node](https://discourse.nodered.org/t/search-for-orphan-nodes/77783) and now I created two nodes to find all pathways between two points in the mind map, I call them [seeker and sink](https://gist.github.com/gorenje/7917848c092cdc08619d9833bd0f0060) (gist link to the code). Basically the seeker traverses all connections (including link-in/out) until it gets to the sink. The seeker then displays twenty-five unique pathways in the tray (the trigger is the `oneditprepare` callback). This is of course a complete hack and something that no one should be doing at home 🙂

But things just get worse. The other day I discovered [Superus](https://www.superusapp.com) and their [ChatGPT in 2D space](https://www.superusapp.com/chatgpt2d/), i.e., they create a concept map using ChatGPT responses. (Aside: the [Hacker News discussion](https://news.ycombinator.com/item?id=35709088) on the idea.) I ended up thinking of doing the same using Node-RED but then thought better of it because I have no idea how to create nodes dynamically within the editor. Searching here in the forum, I realised that it wasn't the intention of the editor (dynamic node creation nor computation).

However ... there must be a way! It happens each time that I drag a node-definition from the palette into the workspace, a node is created via the Node-RED APIs. So I assume there a well defined execution path for creating nodes.

My question is this, can someone give me a pointer to the Node-RED codebase where this actually happens? I could try to find the spot myself but I'd like to save myself the time searching through the codebase 😉

I promise I won't do anything bad with that knowledge!

Thank you for any tips!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 April 2023 15:19 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/2 "2023-04-27T15:19:46Z")

</div>

Well - adding a node is just inserting it's JSON into the flows file, adding a link is then adding the node\_ids into the wires array of the node, and then deploying it (to save it)... of course a) you need to pick what type of node you need, b) set it's x ,y, and z(tab) co-ordinates so that it lays out sensibly, c) assign an ID (so that it can be "wired" into the flow) - and probably a few other things I've forgotten 🙂

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [27 April 2023 15:28 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/3 "2023-04-27T15:28:06Z")

</div>

> [@gregorius](#):
>
> My question is this, can someone give me a pointer to the Node-RED codebase where this actually happens?

Or just use the documented API's?

[https://nodered.org/docs/api/admin/](https://nodered.org/docs/api/admin/)

[https://nodered.org/docs/api/admin/methods/](https://nodered.org/docs/api/admin/methods/)

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 15:40 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/4 "2023-04-27T15:40:28Z")

</div>

> [@marcus-j-davies](#):
>
> Or just use the documented API's?

They affect the server side of things, I would like to do this all in frontend, i.e. in the editor. I don't want to touch the backend ...

Just as an aside: I was thinking about these nodes that I've created, they are a kind of linting:

- orphans are all nodes that aren't connected, that can't be good unless it's comment nodes
- seeker and sink are good for finding whether all pathways are realistic, perhaps there are unexpected links that cause issues

In the case of linting, you want to be doing that before you deploy ... so it would make sense to have nodes that only affect the editor. Kind of pre-deploy checks defined as nodes/flows. 🤔

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 April 2023 15:43 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/5 "2023-04-27T15:43:37Z")

</div>

> [@gregorius](#):
>
> unless it's comment nodes

or ui-template nodes that add things like extra CSS to the `<head>` of the document

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 15:45 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/6 "2023-04-27T15:45:18Z")

</div>

> [@dceejay](#):
>
> set it's x ,y, and z(tab) co-ordinates so that it lays out sensibly,

Coordinates, true that is one thing that I don't have when dynamically adding nodes. (0,0) is probably not the optimal placement 😉 `z` would be the current visible tab ...

Hang on, is the node id perhaps generated on the server side and then passed back to the frontend (i.e. editor)? I know the backend has an id generation for messages ... but is that also used for nodes?

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 15:46 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/7 "2023-04-27T15:46:47Z")

</div>

> [@dceejay](#):
>
> or ui-template nodes that add things like extra CSS to the `<head>` of the document

Ok, so there are definitely more reasons for nodes to be without connections 🙂

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [27 April 2023 15:47 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/8 "2023-04-27T15:47:55Z")

</div>

and indeed config nodes are not "wired" and are not visible in the workspace - but are still nodes...

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 15:54 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/9 "2023-04-27T15:54:38Z")

</div>

> [@dceejay](#):
>
> config nodes are not "wired"

it's starting to sound like nodes that are connected are in the minority 🤔

---

<div class="post-metadata">

**Author:** ![gregorius](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gregorius/32/73816_2.png) [@gregorius](https://discourse.nodered.org/u/gregorius)\
**Post date:** [27 April 2023 16:54 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/10 "2023-04-27T16:54:44Z")

</div>

> [@gregorius](#):
>
> kind of linting

To make it clearer what I mean, I have setup some demonstration flows.

The flows are taken from my [Node-RED as Backend](https://github.com/gorenje/redback) project. It's a complete "backend" for a web application (auction platform) providing an API, web-sockets and email sending when particular actions occur.

As example scenario: I want to know which HTTP (aka API) requests sends out a verification email. First I place the seek node at the email of interest, that is shown in the [email templates flow](https://demo.openmindmap.org/omm/#flow/459c271a96458c7c) (sink node top left). Then I place the seeker node at the front of my [router flow](https://demo.openmindmap.org/omm/#flow/40ea5f2aea6592ae) (aka url mapping flow) where all HTTP requests come in. When I open the tray for the seeker (right side of flow), I see that three possible pathways lead to the verification email. (One for the signup and two for resending a verification code.)

I find this incredibly useful for finding all usages of a node or pathway that I might not have realised. This is also very useful if a product person would like to know when these emails are sent.

Since these nodes only act within the editor, there is no need to deploy them to the server (and they work in read-only mode). This for me makes them a kind of linting or sanity check before doing a deploy.

---

<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:** [26 June 2023 16:54 UTC](https://discourse.nodered.org/t/dynamic-node-creation-in-the-node-red-editor/77983/11 "2023-06-26T16:54:46Z")

</div>

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