# Node-RED for code generation

**URL:** <https://discourse.nodered.org/t/node-red-for-code-generation/67683>\
**Category:** General\
**Created:** [14 September 2022 08:02 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683 "2022-09-14T08:02:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesd256](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jamesd256](https://discourse.nodered.org/u/jamesd256)\
**Post date:** [14 September 2022 08:02 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/1 "2022-09-14T08:02:15Z")

</div>

Has anyone else thought of, or attempted to use NR as a visual code generator for an external language?

In my case of interest that would be Lua. However the application for this I have in mind might work if Node-RED could produce Typescript as there is a transpilation layer made available.

The inspiration for this is first, having completed a project with NR and been inspired by the possibilities, and second watching my kids learn to 'code' with Scratch.

Incidentally, my searches brought me right back here when I found this really fun and clever hack to break open Scratch and connect it to node:

> [@Scratch 3 and NodeRED working together](https://discourse.nodered.org/t/scratch-3-and-nodered-working-together/8473/4):
>
> Hi Simon, Just been trying to get "Translate" to work. Will it work with the Desktop offline version of Scratch-3 ??? I might have misunderstood "Translate". I thought I could send/receive text strings to/from Scratch (and Node-RED) that I could then decode in Scratch to perform things like... move the cat, utter words, etc..

Maybe there's even something possible that could recruit / hijack Scratch into this plan for its UI/codegen.

Separate but related concern, is Node-RED going to hit blockers as a code gen tool? Scratch has been designed to create code and might be the minimum level of similarity to code, but what I see in NR is the possibility at least to craft one or two page scripts where everything is neatly packaged into functions.

---

<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:** [14 September 2022 09:09 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/2 "2022-09-14T09:09:41Z")

</div>

and flipping it round there is the scratch like [blockly](https://flows.nodered.org/node/node-red-contrib-blockly) node for coding within Node-RED 🙂

---

<div class="post-metadata">

**Author:** ![paulkeates](https://avatars.discourse-cdn.com/v4/letter/p/9de053/32.png) [@paulkeates](https://discourse.nodered.org/u/paulkeates)\
**Post date:** [14 September 2022 12:16 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/3 "2022-09-14T12:16:28Z")

</div>

Beat me to it! Was going to say exactly the same thing.

---

<div class="post-metadata">

**Author:** ![jamesd256](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jamesd256](https://discourse.nodered.org/u/jamesd256)\
**Post date:** [14 September 2022 12:37 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/4 "2022-09-14T12:37:45Z")

</div>

Well that was a slam dunk, awesome stuff. Something to bottom out for sure, thanks chaps. Will report back how I get on with this,

---

<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:** [14 September 2022 13:40 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/5 "2022-09-14T13:40:36Z")

</div>

Note that the node-red-contrib-blockly node only generates Javascript code, which is then executed in a NodeJs vm (identical to the function-node). There is no way to generate other languages with it, like you can do with standard Blockly. I only developed the blockly node, to allow people (with less Javascript knowledge) to develop their own custom functions, and execute the generated Javascript code automatically.

---

<div class="post-metadata">

**Author:** ![jamesd256](https://avatars.discourse-cdn.com/v4/letter/j/ecccb3/32.png) [@jamesd256](https://discourse.nodered.org/u/jamesd256)\
**Post date:** [14 September 2022 15:38 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/7 "2022-09-14T15:38:09Z")

</div>

Oops, deleted my post by mistake. Nevermind it was a load of waffle but did help me think about things a bit. TLDR, I'm going to experiment with your excellent solution @BartButenaers.

It may be possible to transpile from JS to Lua quite reliably, but first I'm going to need to look at how to add library modules to node-red-contrib-blockly. Happy to get into the code, so we'll see what can be achieved

---

<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:** [14 September 2022 17:58 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/8 "2022-09-14T17:58:18Z")

</div>

At the time being I have implelented [this](https://github.com/bartbutenaers/node-red-contrib-blockly/issues/76) issue, to allow people to create their own blocks. And those blocks are added to the toolbar. So I assume you can create your own blocks, which generate code. So I assume you can generate code for another language.

However that is only about code generation. From here on the generated code is injected into the vm. And to be honest I want the code and the config screen of this node as simple as possible. So please discuss your proposals before you start implementing.

---

<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:** [28 September 2022 17:59 UTC](https://discourse.nodered.org/t/node-red-for-code-generation/67683/9 "2022-09-28T17:59:00Z")

</div>

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