# How to get the line number of a CSV file in function node

**URL:** <https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163>\
**Category:** General\
**Created:** [22 September 2020 07:12 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163 "2020-09-22T07:12:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [22 September 2020 07:12 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/1 "2020-09-22T07:12:00Z")

</div>

I would like to get the line number of a CSV file in `function` node.

I tried to execute the code below in `function` node but couldn't get the **count** value.  
Do not know where the cause is?

 ![get-line-number-file](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a54b0cbbbdf2bb7b0d1f039bd8cb179044b6921.png)

```auto
[{"id":"61e780fd.ae573","type":"function","z":"e62775a6.462558","name":"","func":"var fs = global.get('fs');\nvar readline = global.get(\"readline\");\nvar filepath = \"/Temp/TestFile.CSV\";\n\n// line number of a CSV file\nvar count = 0;\n\nvar rd = readline.createInterface({\n input: fs.createReadStream(filepath),\n crlfDelay: Infinity\n});\n\nrd.on('line', function(line) {\n count++;\n node.warn(\"count Inside: \" + count);\n});\n\nnode.warn(\"■ ■ count OutSide: \" + count);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":1120,"wires":[["340867e7.bdde68"]]},{"id":"340867e7.bdde68","type":"debug","z":"e62775a6.462558","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":1120,"wires":[]},{"id":"9aa20a11.74a0a8","type":"inject","z":"e62775a6.462558","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":220,"y":1120,"wires":[["61e780fd.ae573"]]}]

```

---

<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:** [22 September 2020 07:21 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/2 "2020-09-22T07:21:48Z")

</div>

Where is readline coming from ? Is it a core module or the deprecated npm package ?

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [22 September 2020 07:25 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/3 "2020-09-22T07:25:09Z")

</div>

I declared it in the `settings.js` file:

```auto
functionGlobalContext: {
	fs:require('fs'),
	readline:require('readline')
}

```

> [@dceejay](#):
>
> Where is readline coming from ? Is it a core module or the deprecated npm package ?

I don't know, can use it after making the above declaration.

---

<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:** [22 September 2020 07:47 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/4 "2020-09-22T07:47:32Z")

</div>

Hi yes it's fine. So you need to understand what your function is doing... when you see a line with `.on` it is only setting up a listener - it is not a loop. It just tells the system to listen for an event - in this case it is asking `rd` to tell it whenever it sees a `line` event. The function then proceeds to the next line and prints out the warning and the count that at that point is still 0. The readline process then tells you every time it gets a new line and the ".on" callback function you have there is called which adds to the count and prints that out...

What you also ned to do is listen for the `close` event that tells you when the file has finished reading - and then in that callback you can print out the final count.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [22 September 2020 08:17 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/5 "2020-09-22T08:17:55Z")

</div>

I added the `close` event and still can't get the **OutSide count**  
I don't understand what you say is a callback...

```auto
// line number of a CSV file
var count = 0;
var countA = 0;

var rd = readline.createInterface({
    input: fs.createReadStream(filepath),
    crlfDelay: Infinity,
    //output: count
});

rd.on('line', function(line) {
    count++;
    //node.warn("count Inside: " + count);
});

rd.on('close', () => {
    node.warn("count Inside: " + count); // Here got the final count OK
    countA = count;
    //context.set('AAA',count);
});

node.warn("■ ■ count OutSide: " + countA);
//node.warn("■ ■ count OutSide: " + context.get('AAA'));

```

---

<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:** [22 September 2020 17:28 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/6 "2020-09-22T17:28:35Z")

</div>

You have to print the output from _within_ the on close function.

The `on` functions are run asynchronously. That is to say that they are not run as part of the standard flow of your function node. They could happen at any time.

Imagine having a file containing 10 million lines. The on 'line' function would chug away for hours but the node-warn would be executed immediately.

---

<div class="post-metadata">

**Author:** ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)\
**Post date:** [22 September 2020 17:37 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/7 "2020-09-22T17:37:33Z")

</div>

> [@TotallyInformation](#):
>
> You have to print the output from _within_ the on close function.

Does this mean I with this approach cannot get the value of the variable **count** out of `rd`?

---

<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:** [22 September 2020 19:00 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/8 "2020-09-22T19:00:38Z")

</div>

Nope. It means that you have to put a node.send inside the on close function and remove the return msg line from the end of your function node.

Getting used to Node.js's async processing takes some time. I recommend that you look for a tutorial.

---

<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:** [21 November 2020 19:00 UTC](https://discourse.nodered.org/t/how-to-get-the-line-number-of-a-csv-file-in-function-node/33163/9 "2020-11-21T19:00:45Z")

</div>

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