# \[Feature Request\]Allow one-liner JavaScript as well as JSONata

**URL:** <https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825>\
**Category:** Feature Requests\
**Created:** [13 November 2023 11:47 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825 "2023-11-13T11:47:51Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [13 November 2023 23:18 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/21 "2023-11-13T23:18:57Z")

</div>

Bash is not very cross platform is it. You couldn’t run that flow on quite a few other platforms..

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 November 2023 23:43 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/22 "2023-11-13T23:43:16Z")

</div>

That's completely right Dave, a Node-red application written with that code would be confined to Linux.

Just like a Node-red application with that code in an exec node.  
Or with C:/myfiles/picture.png in a write-file node.

---

<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:** [13 November 2023 23:57 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/23 "2023-11-13T23:57:40Z")

</div>

So use an exec node then. I guess I’m not seeing why cramming the exec node functionality into the change node is a good idea. The exec node is dangerous enough already. See recent hacks. Such that I think it may be best to disable it by default. Wouldn’t want to bring that exposure to another node.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 November 2023 00:05 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/24 "2023-11-14T00:05:32Z")

</div>

> [@dceejay](#):
>
> I think it may be best to disable [exec] by default.

I agree, especially since Node-red has not been patched to alleviate the risk of malicious exec code on systems with passwordless sudo.

But that is a spurious argument in the context of my suggestion above, which would also be disabled by default. I dare say a function node could be made harmful, or the (horrible) contrib Python node.

I know you don't see the advantage of my suggestions. I'm sorry I can't convince you of their value. Ah well, it's your language not mine.

---

<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:** [14 November 2023 00:07 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/25 "2023-11-14T00:07:25Z")

</div>

> [@jbudd](#):
>
> Can you explain more about external services and external code module linked into globals?

There are any number of languages that can create microservices. node.js, Python, Go, Ruby, .... A microservice can deliver/consume MQTT, TCP, UDP, UNIX Pipes and probably many other types of interconnections.

For the globals, all you need is a node.js module and simply `require` it into the globals in settings.js - that code/data is then available in Node-RED in functions or custom nodes. or (for data anyway) even from change nodes, etc. A node.js module is simply a file that `exports` some stuff.

> [@jbudd](#):
>
> Here is an example

There are so many different ways to do this kind of thing. For example, I have some scripts that run on CRON schedules that call out to a Node-RED endpoint (http-in/-out). But of course, you could easily use a command-line MQTT tool or many other ways.

If you weren't doing a command-line thing at its heart, then a more powerful scripting language like Python or node.js, etc. could do a lot more than BASH.

> [@dceejay](#):
>
> Bash is not very cross platform is it.

But PowerShell can now run cross-platform. 😉

> [@dceejay](#):
>
> I guess I’m not seeing why cramming the exec node functionality into the change node is a good idea

No, it really wouldn't be a good idea at all! It would be really hard to debug or even to understand after a month. It would likely also be a security nightmare.

JSONata is constrained to a smallish subset of processing. Basically a subset of a single(ish) data input and output. At most, this JS idea should be similarly constrained. Anything more would at best invite some really poor design practices.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 November 2023 00:15 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/26 "2023-11-14T00:15:45Z")

</div>

> [@TotallyInformation](#):
>
> If you weren't doing a command-line thing at its heart, then a more powerful scripting language like Python or node.js, etc. could do a lot more than BASH.

Perhaps you evaded the point that my example was confined to bash but the suggestion does not specify any language.

A more powerful scripting language could do a lot more than I (or @cymplecy) like to do in Javascript.

I'll shut up again now 😀

---

<div class="post-metadata">

**Author:** ![sharonv](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@sharonv](https://discourse.nodered.org/u/sharonv)\
**Post date:** [14 November 2023 14:43 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/27 "2023-11-14T14:43:22Z")

</div>

Jsonata can use external libraries quite easily, maybe having quick access to such includes would give you the benefit of using external code. Additionally, jsonata has the ability to register functions ("registerFunction" that encapsulates some functionality onto a custom command, maybe an interface to this method.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b05f357795ab76f7d852366804872a6c1d924c90.png)

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [14 November 2023 18:59 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/28 "2023-11-14T18:59:58Z")

</div>

Excellent - seems to work quite nicely 🙂  
Could I persuade you to make up a switch node version as well? 🙂

---

<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:** [14 November 2023 19:20 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/29 "2023-11-14T19:20:22Z")

</div>

And not forgetting an inject node one! 🤯

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [14 November 2023 19:52 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/30 "2023-11-14T19:52:34Z")

</div>

You can use the change node after an inject node for testing

---

<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 November 2023 19:57 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/31 "2023-11-14T19:57:36Z")

</div>

That is the point of Node-RED. To link up simple nodes that do their thing well, and not to cram every capability into every node.

---

<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:** [14 November 2023 20:10 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/32 "2023-11-14T20:10:18Z")

</div>

Spoilsport! 🙂

---

<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 November 2023 20:12 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/33 "2023-11-14T20:12:38Z")

</div>

That’s my job 😉

---

<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:** [14 November 2023 20:28 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/34 "2023-11-14T20:28:09Z")

</div>

However, I do hope that the ability is being baked into the standard typed input options? That way, we can all benefit from it. 🙂

---

<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:** [14 November 2023 21:00 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/35 "2023-11-14T21:00:31Z")

</div>

> [@cymplecy](#):
>
> Excellent - seems to work quite nicely

Thank you!  
I just pushed an update to the repo that should perform now as well nice syntax checking.

> [@cymplecy](#):
>
> Could I persuade you to make up a switch node version as well? 🙂

Well, this little PoC shows that it's not overly complicated to get the discussed functionality into the core nodes.  
It all comes down thus to the question if this feature attracts enough fans to persuade the community / maintainers to accept a PR. I'd definitely not vote for (or maintain) a (or another) parallel eco system of alternative change / switch / inject nodes...

**Personal perception:**  
Once you get used to it a bit, it feels so easy & much more intuitive than any other option...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f658cfa1a42ba6e4822c41fe05b340414a5aa88.png)

---

<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:** [14 November 2023 21:11 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/36 "2023-11-14T21:11:34Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d44de354377fa193e2a14afb4da96ed10266f1d8.png)

If it makes it into core, I'll be using this all the time to set random values for testing flows and nodes - currently I have to use

```auto
$formatInteger($random() * 100, "0")

```

Using JSONata - super annoying.

---

<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:** [14 November 2023 21:20 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/37 "2023-11-14T21:20:12Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/300ffc591641c109ed787548cf98238c295e0d79.png)

Easier, yet not shorter...

---

<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:** [14 November 2023 21:25 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/38 "2023-11-14T21:25:56Z")

</div>

True, but hopefully a lot faster. And no annoying `$` signs 🤣

Oh, and of course, it returns an actual number not text.

---

<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 November 2023 21:47 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/39 "2023-11-14T21:47:12Z")

</div>

Math.floor etc I can get behind. But once you start allowing function definitions then why not use a function node that you can at least document rather than hide behind one line ?

And can you then also set other msg properties within the same call ? Gets confusing real quick. And do you then allow node.warn for debugging etc…

---

<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:** [14 November 2023 22:40 UTC](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825/40 "2023-11-14T22:40:55Z")

</div>

> [@dceejay](#):
>
> But once you start allowing function definitions then why not use a function node that you can at least document rather than hide behind one line ?

I'm pretty convinced, you can do crazy stuff with JSONata as well. People don't do this (usually) not because they're unfamiliar with JSONata, but because it's _crazy stuff_. On the other hand it feels very convenient to write a small function to process a value _in-line_ within a `change` node.  
Not sure yet that I get your point regarding documentation. Of course there's the standard JS editor opening when you press the `...` button. Agreed, the script doesn't look too nice in the small `input` box - as does a JSON structure or longer JSONata statement. Once we've clarified the 'if', there'll be time to investigate the best way to represent things...

> [@dceejay](#):
>
> And do you then allow node.warn for debugging etc…

Functionality can always be discussed. Yet ... why shouldn't that be possible and helpful?

[Previous page](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825.md?page=1)

[Next page](https://discourse.nodered.org/t/feature-request-allow-one-liner-javascript-as-well-as-jsonata/82825.md?page=3)
