# 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:** 6\
**Page:** 2

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

</div>

Might lend a clue to the randomness of it  
Likely needs a bit off tweaking 🤷‍♂️

---

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

</div>

> [@Mike-FUT](#):
>
> Example flow:

Can see an error on importing that node - but nothing that reports the lint error you are seeing.

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

Opening, pressing return (anywhere) and pressing done clears the error.

I can't find a way that re-introduces an error.

---

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

</div>

Bug filed...

> <https://github.com/node-red/node-red/issues/4494>
>
> \### Current Behavior
> 
> In function nodes when declaring variables with same name …in "On Start" and "On Message" tabs the editor will underline the variable names and show the error message "Cannot redeclare block-scoped variable".
> This happens with "var", "let" and "const".
> When finishing the function node with "OK" button there will be a red dot on the function indicating an error. When deploying there is a warning before it's actually deployed.
> But it still works perfectly so it's mainly an annoying/cosmetical issue.
> This issue is not reproducible by everyone, see here: https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937
> The bug has been out there for ages, I guess since beginning of v3. In the forum one reported it on 3.0.2 and node 20.
> 
> \### Expected Behavior
> 
> Variables with identical names can be declared in both "On Start" and "On Message" without throwing errors.
> 
> \### Steps To Reproduce
> 
> !\[nr bug\](https://github.com/node-red/node-red/assets/61519363/317ff662-1f38-48f3-ae6e-570d1f58fa2a)
> 
> 
> \### Example flow
> 
> \`\`\`
> \[
> {
> "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": \[
> \[\]
> \]
> }
> \]
> \`\`\`
> 
> 
> \### Environment
> 
> \- Node-RED version: 3.1.3
> \- Node.js version: 18.19.0
> \- npm version: 10.2.3
> \- Platform/OS: Ubuntu 20.04.6 LTS (GNU/Linux 5.4.0-169-generic x86\_64)
> \- Browser: Firefox 121 and Chrome 120
> (a lot other Node.js, Node RED and browser versions have been reported to be affected, see linked forum post)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 December 2023 21:42 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/24 "2023-12-21T21:42:21Z")

</div>

Yep, I remember the pain of working around that when I added Monaco editor.

It's likely something has changed over the years and the flakey work around of detecting the editor being out of focus and switching models is a bit broken.

Not looking forward to digging into it. Thanks guys.

---

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

</div>

Sorry for that... 😇  
Luckily it's only an annoying bug - either choose different variable names for exact same data points or be prepared to acknowledge the error messages. But it's not causing any functional issues.  
For me the prio is not too high, I've gotten used to it. I still wanted to raise it though.

---

<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:** [19 February 2024 21:58 UTC](https://discourse.nodered.org/t/bug-in-function-editor-cannot-redeclare-block-scoped-variable/83937/26 "2024-02-19T21:58:25Z")

</div>

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

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