# Is this no longer valid practice?

**URL:** https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517
**Category:** Developing Nodes
**Created:** [22 July 2020 00:28 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517 "2020-07-22T00:28:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [22 July 2020 00:28 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/1 "2020-07-22T00:28:08Z")

</div>

In a custom node, on input just as a test...

```auto
const sleep = require('sleep');
//
sleep.sleep(5000);

```

I did load sleep via npm...

```auto
$ npm list | grep sleep
└─┬ sleep@6.3.0

```

Log shows...

```auto
22 Jul 00:21:14 - [error] [si7021:ffcfd507.410ae8] Error: Cannot find module 'sleep'

```

Am I doing something wrong or what? Do I have to add the sleep node the global function in settings.js, when I am just using it for code development?

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [22 July 2020 05:26 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/2 "2020-07-22T05:26:55Z")

</div>

Did you add sleep to the dependencies in your package.json? You need to do this for all non core node modules you require.  
_PS_  
did you install sleep globally? because it needs to be installed from the .node-red folder same as node-red-nodes. If you have it in your package json it will get installed automatically when you install your node.  
_ **PPS** _  
I would really not recommend to use the sleep module in any production node or node you plan to distribute. Due to node.js’s single threaded nature this will stop the **complete event loop** for the sleep period. So all other nodes doing work in the runtime would be stuck too for the sleep period if you call it from your node. Rather implement something with setTimeout() and its callback function if you need to do time delayed work as this way you are not blocking the whole event loop.

Johannes

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [22 July 2020 06:07 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/3 "2020-07-22T06:07:01Z")

</div>

> [@JGKK](#):
>
> Rather implement something with setTimeout()

Or just use the `delay` node. 😀

---

<div class="post-metadata">

### Author: ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)
#### Post date: [22 July 2020 06:08 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/4 "2020-07-22T06:08:28Z")

</div>

He cant do that in a custom node 🙃 and the delay node implements its functionality with `setTimeout()` and `setInterval()` too.

---

<div class="post-metadata">

### Author: ![kuema](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kuema/32/6542_2.png) [@kuema](https://discourse.nodered.org/u/kuema)
#### Post date: [22 July 2020 06:11 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/5 "2020-07-22T06:11:32Z")

</div>

Oops, misread that... caffeine deficit. 🙈 Will apply a hot-fix. ☕

---

<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 July 2020 07:47 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/6 "2020-07-22T07:47:20Z")

</div>

I vote with @JGKK. Don't use an extra module (especially if it blocks) when you can use setTimeout etc.

---

<div class="post-metadata">

### Author: ![Nodi.Rubrum](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nodi.rubrum/32/107482_2.png) [@Nodi.Rubrum](https://discourse.nodered.org/u/Nodi.Rubrum)
#### Post date: [22 July 2020 12:54 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/7 "2020-07-22T12:54:38Z")

</div>

I am or was only going to use it during learning/development. Moreover, since setTimeout() is non-blocking, I would have code continue when I really did not want it to while testing. I am writing custom nodes, debugging the node its-self, so delay node is not an option. I always install modules under .node-red, given I am wring custom nodes.

---

<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 September 2020 12:54 UTC](https://discourse.nodered.org/t/is-this-no-longer-valid-practice/30517/8 "2020-09-20T12:54:40Z")

</div>

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