# Watching directory with fs.watch not closing prev. watchers

**URL:** <https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947>\
**Category:** Developing Nodes\
**Created:** [18 January 2019 07:45 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947 "2019-01-18T07:45:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simo219h](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simo219h/32/7229_2.png) [@Simo219h](https://discourse.nodered.org/u/Simo219h)\
**Post date:** [18 January 2019 07:45 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/1 "2019-01-18T07:45:17Z")

</div>

Hi,

I am struggling with a file that I am trying to watch for changes. I use the fs.watch, and listening on the 'change' event. It works fine, but when I hit the "Deploy" button in node-red, it opens another watcher, if I hit "Deploy" again, a third watcher appear. (Bonus info - i am running Linux). It seems it doesnt close the previously watchers. Do you have any suggestions?

Here is my .js code:

```
fs.watch(doPath, (eventType, filename) => {
        var msg = {payload: ""};
        if(eventType === 'change') {
            fs.readFile(doPath, 'utf8', (err, data) => {
                if (err) throw err;
                if(data[0] == 1) {
                    msg.payload = true;
                    node.send(msg);                              
                    node.status({fill:"green",shape:"dot",text:"digital-outputs.status.on"});       
                }
                else if (data[0] == 0) {                       
                    node.status({fill:"red",shape:"dot",text:"digital-outputs.status.off"});
                    msg.payload = false;
                    node.send(msg);
                }
            });
        }
    });
```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 January 2019 08:11 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/2 "2019-01-18T08:11:24Z")

</div>

Have you added code to handle the close event on the node in order to remove the watcher? That won't happen automatically - you have to tidy up resources you create.

---

<div class="post-metadata">

**Author:** ![Simo219h](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simo219h/32/7229_2.png) [@Simo219h](https://discourse.nodered.org/u/Simo219h)\
**Post date:** [18 January 2019 08:13 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/3 "2019-01-18T08:13:59Z")

</div>

No, I have not. But I only want to close the watcher when I hit "deploy". Where will the correct place be, to implement that? 🙂

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [18 January 2019 08:16 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/4 "2019-01-18T08:16:16Z")

</div>

> [@Simo219h](#):
>
> Where will the correct place be, to implement that?

In the close event handler on the node: [JavaScript file : Node-RED](https://nodered.org/docs/creating-nodes/node-js#closing-the-node)

---

<div class="post-metadata">

**Author:** ![Simo219h](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simo219h/32/7229_2.png) [@Simo219h](https://discourse.nodered.org/u/Simo219h)\
**Post date:** [18 January 2019 08:16 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/5 "2019-01-18T08:16:55Z")

</div>

aaah, genius. Thanks!

---

<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:** [18 January 2019 13:43 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/6 "2019-01-18T13:43:10Z")

</div>

Though we do have watch node that does this already... does it not work for you use case ?

---

<div class="post-metadata">

**Author:** ![Simo219h](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/simo219h/32/7229_2.png) [@Simo219h](https://discourse.nodered.org/u/Simo219h)\
**Post date:** [18 January 2019 13:45 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/7 "2019-01-18T13:45:38Z")

</div>

Maybe, but I want to make a custom node 😉

I got it working now 🙂

---

<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:** [18 January 2019 13:59 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/8 "2019-01-18T13:59:41Z")

</div>

No problem - all good learning experience.  
(but if there is problem with the existing node please let us know)

---

<div class="post-metadata">

**Author:** ![crolshoven](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/crolshoven/32/12366_2.png) [@crolshoven](https://discourse.nodered.org/u/crolshoven)\
**Post date:** [22 September 2019 10:16 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/9 "2019-09-22T10:16:22Z")

</div>

Hi,  
Is it possible to use the watch node on network shares?

Thanks

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [22 September 2019 11:53 UTC](https://discourse.nodered.org/t/watching-directory-with-fs-watch-not-closing-prev-watchers/6947/10 "2019-09-22T11:53:33Z")

</div>

Please do not post multiple times about the same issue in different places. You get better help if you keep it at one place, and won’t get several people researching/suggesting the same thing attempting to help.

> [@Watch node on network path](https://discourse.nodered.org/t/watch-node-on-network-path/15773):
>
> Hi, Is it possible to configure the watch node with credentials to watch a folder on a network path? What would be an alternative solution, if this is not possible? Thanks for the help in advanced. Regards,
