Is this no longer valid practice?

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

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

I did load sleep via npm...

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

Log shows...

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?

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

Or just use the delay node. :grinning:

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

Oops, misread that... caffeine deficit. :see_no_evil: Will apply a hot-fix. :coffee:

3 Likes

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

4 Likes

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.

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