# Code does not work in function node but works fine when run separately

**URL:** <https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297>\
**Category:** General\
**Created:** [26 May 2021 17:45 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297 "2021-05-26T17:45:43Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 17:45 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/1 "2021-05-26T17:45:44Z")

</div>

Hi friends, I am using ExcelJS to write array to excel. and it's working fine when i run it using node exceltest.js

> const ExcelJS = require('exceljs');
> 
> exceltest()
> 
> async function exceltest(){  
> try{  
> const workbook = new ExcelJS.Workbook();  
> const sheet = workbook.addWorksheet('contacts\_company');
> 
> ```
> sheet.columns = [{ header: 'company', key: 'company', width: 10}, 
> { header: 'contact', key: 'contact', width: 10}, { header: 'country', key: 'country', width: 10}]
>         
>        
> sheet.addRows([['Alfreds Futterkiste', 'Maria Anders', 'Germany'],['Centro comercial Moctezuma', 'Francisco Chang', 'Mexico'], ['Ernst Handel', 'Roland Mendel', 'Austria'], ['Island Trading', 'Helen Bennett', 'UK'], ['Laughing Bacchus Winecellars', 'Yoshi Tannamuri', 'Canada'], ['Magazzini Alimentari Riuniti', 'Giovanni Rovelli', 'Italy']]);
>        
> await workbook.xlsx.writeFile("./writtenbyexceljs.xlsx")
>       
> }catch(err){
> console.log("Error is ------------ ", err)
> }
> 
> ```
> 
> }

Output:

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

But, when I put the same code in a function node (after adding the require in settings.js file and using it as global.get), It doesn't add the rows anymore

Output:

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

Any help on why it doesn't work inside function node is appreciated

---

<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:** [26 May 2021 18:34 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/2 "2021-05-26T18:34:51Z")

</div>

> [@karthikraja](#):
>
> It doesn't add the rows anymore

Are you saying it builds an Excel spreadsheet and adds the top row?

Perhaps there is already an existing (possibly locked) xls file and it's not overwriting ?

Instead of `console.log("Error...")` put `node.error(err,msg)` in the catch block - and check see if you get any errors in node-red debug window?

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 19:26 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/3 "2021-05-26T19:26:45Z")

</div>

Thanks for the reply

> [@Steve-Mcl](#):
>
> Perhaps there is already an existing (possibly locked) xls file and it's not overwriting ?

I checked it and deleted the file to make sure and also changed the name of the file in the script

> [@Steve-Mcl](#):
>
> Instead of `console.log("Error...")` put `node.error(err,msg)` in the catch block - and check see if you get any errors in node-red debug window?

No errors. Flow executes without any error... This makes me break my head 🤪

---

<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:** [26 May 2021 19:33 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/4 "2021-05-26T19:33:03Z")

</div>

> [@Steve-Mcl](#):
>
> Are you saying it builds an Excel spreadsheet and adds the top row?

↑ what about this question? ↑

---

<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:** [26 May 2021 19:34 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/5 "2021-05-26T19:34:06Z")

</div>

In addition to Steve's question. Replace the relative file path with an absolute file path because that is a very common error where you aren't looking at the file you think you are.

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 19:35 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/6 "2021-05-26T19:35:54Z")

</div>

> [@Steve-Mcl](#):
>
> Are you saying it builds an Excel spreadsheet and adds the top row?

Sorry, missed it. Yes, Excel spreadsheet is written with the top header

---

<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:** [26 May 2021 19:41 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/7 "2021-05-26T19:41:32Z")

</div>

Have you tried singular `addRow`

Have you tried with named props in an object (instead of arrays)?

Have you tried the non async way?

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 19:43 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/8 "2021-05-26T19:43:42Z")

</div>

> [@TotallyInformation](#):
>
> Replace the relative file path with an absolute file path because that is a very common error where you aren't looking at the file you think you are.

I tried this now, but with the same observation.

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 19:50 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/9 "2021-05-26T19:50:22Z")

</div>

> [@Steve-Mcl](#):
>
> Have you tried singular `addRow`

Yes, again it's not added.

> [@Steve-Mcl](#):
>
> Have you tried with named props in an object (instead of arrays)?

Yes, this works fine. For example If I use

> worksheet.addRow({company: 'Alfreds Futterkiste', contact: 'Maria Anders', country: 'Germany'});

Output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/aceb3e452d050ef1ae59d7cc34ba18595693d43e.png)

> [@Steve-Mcl](#):
>
> Have you tried the non async way?

Am not sure if that means, removing the function and awaits and adding the commands one after another. I did try that, but same issue.

---

<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:** [26 May 2021 20:09 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/10 "2021-05-26T20:09:03Z")

</div>

> [@karthikraja](#):
>
> Yes, this works fine. For example If I use
> 
> > worksheet.addRow({company: 'Alfreds Futterkiste', contact: 'Maria Anders', country: 'Germany'});

Then go with that 🙂

> [@karthikraja](#):
>
> > [@Steve-Mcl](#):
> >
> > Have you tried the non async way?
> 
> Am not sure if that means, removing the function and awaits and adding the commands one after another. I did try that, but same issue.

The examples [in the readme](https://www.npmjs.com/package/exceljs#add-rows) of exceljs has a few examples NOT using async await

  

Out of interest, was [node-red-contrib-excelsheets](https://flows.nodered.org/node/node-red-contrib-excelsheets) no good for you or do you need to use exceljs?

---

<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:** [26 May 2021 20:12 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/11 "2021-05-26T20:12:15Z")

</div>

I tried with addRows and it doesn't work. I tried with addTable and it does work. Tables are better anyway when working with Excel.

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 20:25 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/12 "2021-05-26T20:25:17Z")

</div>

> [@Steve-Mcl](#):
>
> Then go with that 🙂

Yes, that's what I ended up doing, but I just wanted to know if I was doing anything wrong.

> [@Steve-Mcl](#):
>
> The examples [in the readme](https://www.npmjs.com/package/exceljs#add-rows) of exceljs has a few examples NOT using async await

I will try this for sure and update. Thanks for the same.

> [@Steve-Mcl](#):
>
> Out of interest, was [node-red-contrib-excelsheets](https://flows.nodered.org/node/node-red-contrib-excelsheets) no good for you or do you need to use exceljs?

Not like that.., am just trying things out. and learning ....Will try with node-red-contrib-excelsheets also.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [26 May 2021 20:31 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/13 "2021-05-26T20:31:11Z")

</div>

It works well if you define the rows by using the keys from header.

```auto
sheet.columns = [
  	    { header: 'company', key: 'company', width: 10},
  	    { header: 'contact', key: 'contact', width: 10}, 
  	    { header: 'country', key: 'country', width: 10}
  	]
        
   let rows = [
       {company:'The company',contact:'200-300',country:'foo'},
       {company:'Other company',contact:'234-300',country:'fii'}
   ]
    const newrows = sheet.addRows(rows);

```

---

<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:** [26 May 2021 20:34 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/14 "2021-05-26T20:34:36Z")

</div>

Still a bug I would guess

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 20:42 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/15 "2021-05-26T20:42:59Z")

</div>

> [@TotallyInformation](#):
>
> I tried with addRows and it doesn't work. I tried with addTable and it does work. Tables are better anyway when working with Excel.

Well, this worked like a charm, and without much changes to code and looks better too

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

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [26 May 2021 20:43 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/16 "2021-05-26T20:43:15Z")

</div>

Probably. Maybe depends on something like OS or ... who knows  
[addRow by array don't work? · Issue #530 · exceljs/exceljs · GitHub](https://github.com/exceljs/exceljs/issues/530)

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 20:43 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/17 "2021-05-26T20:43:48Z")

</div>

> [@hotNipi](#):
>
> It works well if you define the rows by using the keys from header.

Yes, but as per the document, array should work too (without the keys)

---

<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:** [26 May 2021 20:44 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/18 "2021-05-26T20:44:12Z")

</div>

Probably the biggest mistake people make with Excel is not using tables.

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 20:45 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/19 "2021-05-26T20:45:37Z")

</div>

Might be. But it I faced the issue only when I run it from inside node-red 😄

---

<div class="post-metadata">

**Author:** ![karthikraja](https://avatars.discourse-cdn.com/v4/letter/k/e274bd/32.png) [@karthikraja](https://discourse.nodered.org/u/karthikraja)\
**Post date:** [26 May 2021 20:46 UTC](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297/20 "2021-05-26T20:46:12Z")

</div>

Well I will use them from now, for sure. Thanks again 😀

[Next page](https://discourse.nodered.org/t/code-does-not-work-in-function-node-but-works-fine-when-run-separately/46297.md?page=2)
