# Bug in function editor (Cannot redeclare block-scoped variable)

**URL:** <https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937>\
**Category:** General\
**Tags:** function-node\
**Created:** [21 December 2023 19:50 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937 "2023-12-21T19:50:09Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 19:50 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/1 "2023-12-21T19:50:09Z")

</div>

Hi All,

I'm surprised I didn't find anyone else to report this bug:  
When using constants or variables in a function node with the same name both at the "On Start" and "On Message" section the editor throws an error message `Cannot redeclare block-scoped variable` and does a red underline below the variables.  
I tried "const", "let" and "var". The error will show up always.  
Still I can save and successfully run the node so it's only cosmetical but it might be worth fixing.

Or is the whole thing intended behavior and I'm to blind to see the reason for it and that's why I can't find anyone else reporting that bug?

---

<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:** [21 December 2023 19:57 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/2 "2023-12-21T19:57:27Z")

</div>

> [@Mike-FUT](#):
>
> The error will show up always.

I tested this with `ace` as well as `monaco` - but I cannot confirm:

```auto
[
    {
        "id": "d0fd04357f17d46f",
        "type": "function",
        "z": "b808fb4094db08d8",
        "name": "function 33",
        "func": "const test = 18;\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "// Der Code hier wird ausgeführt,\n// wenn der Node gestartet wird\nlet test = 15;",
        "finalize": "// Der Code hier wird ausgeführt,\n// wenn der Node gestoppt wird\nlet test = 19;",
        "libs": [],
        "_mcu": {
            "mcu": false
        },
        "x": 490,
        "y": 120,
        "wires": [
            [
                "b3d26ecbc6d74b59"
            ]
        ]
    },
    {
        "id": "10f17f127a6858bd",
        "type": "inject",
        "z": "b808fb4094db08d8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "_mcu": {
            "mcu": false
        },
        "x": 300,
        "y": 120,
        "wires": [
            [
                "d0fd04357f17d46f"
            ]
        ]
    },
    {
        "id": "b3d26ecbc6d74b59",
        "type": "debug",
        "z": "b808fb4094db08d8",
        "name": "debug 115",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 670,
        "y": 120,
        "wires": []
    }
]

```

Could you please provide a short flow that shows the issue?

---

<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:** [21 December 2023 19:57 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/3 "2023-12-21T19:57:36Z")

</div>

I don't use `monaco `  
but if someone can reproduce this, might be worth raising on GitHub.

@Mike-FUT  
Can you advise of:

- Browser
- Node Version
- Node RED Version

---

<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:** [21 December 2023 20:04 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/4 "2023-12-21T20:04:35Z")

</div>

> [@Mike-FUT](#):
>
> I'm surprised I didn't find anyone else to report this bug

Perhaps because it isn't happening for others? I can't reproduce it I'm afraid. Win11, Vivaldi browser, latest Node-red, node.js v18

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 December 2023 20:11 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/5 "2023-12-21T20:11:39Z")

</div>

I am seeing it  
Node-RED v3.0.2  
Node.js v20.2.0

Not able to access my 3.1.3 server at the moment

 ![Screenshot_20231221-201500](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/893e8c6e1ee21ee368037a709494fab67a9844bc.png)

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:22 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/6 "2023-12-21T20:22:59Z")

</div>

My bad... I forgot to post the details.

Browser: Firefox 121 and Chrome 120  
Node Version: 18.18.2 and 18.19.0 (Npm 10.2.3)  
Node RED Version: 3.1.0 and 3.1.3

Tabs "On Start" and "On Message".  
Sometimes it requires to hit the enter key or other key presses on one tab so the error shows up on the other tab. On the tab where anything is changed the error disappears but still shows up on the other tab.

I believe I have seen this issue already a long while ago.

Example flow:

```auto
[
    {
        "id": "3c780662f9dd4911",
        "type": "function",
        "z": "deb9ccecbf2319fe",
        "name": "function 50",
        "func": "let test=1;\n",
        "outputs": 1,
        "timeout": 0,
        "noerr": 1,
        "initialize": "let test=1;\n",
        "finalize": "",
        "libs": [],
        "x": 530,
        "y": 240,
        "wires": [
            []
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:25 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/7 "2023-12-21T20:25:51Z")

</div>

Looks exactly the same here. It will show a red mark on the function node, it will warn me when deploying, but it works perfectly.

---

<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:** [21 December 2023 20:29 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/8 "2023-12-21T20:29:44Z")

</div>

I am not seeing it (just installed latest in a test rig)

- 3.1.3 | 20.10.0 | Safari

A little inconclusive this one

- Mike (3.1.3)
- E1cid (3.0.2)
- Julian (3.1.3)
- Marcus (3.1.3)
- Jeff (3.1.3)

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [21 December 2023 20:36 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/9 "2023-12-21T20:36:13Z")

</div>

No problem here

3.1.3 | 18 | Edge

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:37 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/10 "2023-12-21T20:37:27Z")

</div>

![nr bug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/8/68d4c76ad1ff79428fc461355e91e46ac0642854.gif)

Brand new created function.

---

<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:** [21 December 2023 20:38 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/11 "2023-12-21T20:38:34Z")

</div>

Is that 3.1.3?

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:39 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/12 "2023-12-21T20:39:15Z")

</div>

It is.  
And I believe it has been like this for ages.

---

<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:** [21 December 2023 20:40 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/13 "2023-12-21T20:40:06Z")

</div>

Odd - I still cant reproduce it

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:41 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/14 "2023-12-21T20:41:10Z")

</div>

Related to the underlaying OS? My understanding about how Node RED works under the hood isn't good enough to judge if that makes sense.  
I'm using: Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-169-generic x86\_64)

Btw, just started Edge that I never use. Same issue with latest MS Chromium Edge. First time I started that on this machine so it can't be related to any cookies, cache or whatever.

---

<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:** [21 December 2023 20:44 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/15 "2023-12-21T20:44:53Z")

</div>

The points off interest will usually be.

- Browser.
- Node RED version.

Node version and OS shouldn't (normally) make a difference, as its UA/Client based

Strange one.

Issues are raised here if you wanted to pursue it : [Issues · node-red/node-red · GitHub](https://github.com/node-red/node-red/issues)  
Ensure to provide your environment detail and what versions you are seeing it against

---

<div class="post-metadata">

**Author:** ![Mike-FUT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mike-fut/32/50736_2.png) [@Mike-FUT](https://discourse.nodered.org/u/Mike-FUT)\
**Post date:** [21 December 2023 20:49 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/16 "2023-12-21T20:49:56Z")

</div>

Thank you, I'll file a bug tomorrow.  
I'll wait a bit more, probably someone has noticed that already and found the reason but considering my memory that it's been out there for ages and E1cid sees that issue on 3.0.2 it could be easily since beginning of v3.  
So to repeat my initial comment: I'm surprised nothing has been reported so far. Luckily I'm not the only one experiencing 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:** [21 December 2023 20:58 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/17 "2023-12-21T20:58:05Z")

</div>

3.1.1 | 16 | Chrome =\> can't reproduce!  
3.1.3 | 18 | Chrome =\> Yeap! See it... ! 👍

---

<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:** [21 December 2023 21:01 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/18 "2023-12-21T21:01:23Z")

</div>

Just for completeness

3.1.3 | 20 | Safari =\> can't reproduce!

reproducibility seems apparent - I would probably raise it

if I am not mistaken

3.1.3 | 20 | Safari  
 3.1.3 | 18 | Vivaldi  
 3.1.1 | 16 | Chrome  
 3.1.3 | 18 | Edge  
 3.1.3 | 18 | Chrome  
 3.1.3 | 20 | Chrome  
 3.1.0 | 18 | FF  
 3.1.3 | 18 | Chrome  
 3.0.2 | 20 | Chrome

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 December 2023 21:08 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/19 "2023-12-21T21:08:04Z")

</div>

I am also seeing in 3.1.3 / 20 / chrome

---

<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:** [21 December 2023 21:13 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/20 "2023-12-21T21:13:31Z")

</div>

There's a comment in the source that might give some hints:

```auto
//as models are signleton, consts and let are avialable to other javascript instances
//so when not focused, set editor mode to text temporarily to avoid multiple defs

```

[Next page](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937.md?page=2)
