# Reading specific line from a file in function node

**URL:** <https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620>\
**Category:** General\
**Created:** [21 October 2020 07:52 UTC](https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620 "2020-10-21T07:52:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![KiwiTrinitrotoluene](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KiwiTrinitrotoluene](https://discourse.nodered.org/u/KiwiTrinitrotoluene)\
**Post date:** [21 October 2020 07:52 UTC](https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620/1 "2020-10-21T07:52:11Z")

</div>

Hello everyone,

i'm kinda new both to node-red and java script and i was trying to log data on a file and then read back a single line from an arbitrary position.

For the first part, i simply used the node-red File node, appending to a file the new data and enabling the "Add newline" option.

For the reading part, after some researching i came to the conclusion that perhaps i could use the node.js module called "nthline". So i installed it globally through npm and then went to node-red settings.js to add the line `nthline:require('nthline')` to the functionGlobalContext section.

Then in the function node of my flow i added:

> var getline = global.get('nthline');
> 
> msg.payload = getline(5, '/home/test/testfile.txt');
> 
> return msg;

but in the debug window, the message the function node outputs has empty payload.

What did i do wrong? Is there maybe a betetr way to read a single specific line from a file?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 October 2020 08:16 UTC](https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620/2 "2020-10-21T08:16:09Z")

</div>

Welcome to the forum @KiwiTrinitrotoluene

nthline doesn't work like that, it returns a Promise which you have to use to wait for the result. Have a look at the readme to see how to use it. [https://www.npmjs.com/package/nthline](https://www.npmjs.com/package/nthline)

---

<div class="post-metadata">

**Author:** ![KiwiTrinitrotoluene](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KiwiTrinitrotoluene](https://discourse.nodered.org/u/KiwiTrinitrotoluene)\
**Post date:** [21 October 2020 12:36 UTC](https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620/3 "2020-10-21T12:36:29Z")

</div>

Oh, thanks for pointing it out! I missed somehow how a Promise works.

---

<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:** [20 December 2020 12:36 UTC](https://discourse.nodered.org/t/reading-specific-line-from-a-file-in-function-node/34620/4 "2020-12-20T12:36:31Z")

</div>

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