# How to activate the Global Hooks

**URL:** <https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251>\
**Category:** General\
**Created:** [7 May 2023 18:54 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251 "2023-05-07T18:54:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [7 May 2023 18:54 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/1 "2023-05-07T18:54:23Z")

</div>

Hi !  
I would like to activate the global hooks as described in [Hooks API : Node-RED](https://nodered.org/docs/api/hooks/).

After spending hours and hours searching for any example how to activate them - has anybody any deeper information how to do this ?

Where the "RED.hooks.add(" has to be configured ? Any example or deeper description availiable which I've not found ?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 May 2023 06:19 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/2 "2023-05-08T06:19:10Z")

</div>

Hooks can be used in custom nodes;

> _Note: there is also a hooks api in the editor, but it is less mature so not currently documented for general use._

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [8 May 2023 13:48 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/3 "2023-05-08T13:48:22Z")

</div>

Thanks für your reply. But this one are not the hooks I mean.

I mean the GLOBAL ones - for example "onSend" which will be fired on every Send event in all nodes in all flows (if my understanding of the documentation is right) .

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [8 May 2023 18:42 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/4 "2023-05-08T18:42:16Z")

</div>

> [@nru2023](#):
>
> I mean the GLOBAL ones - for example "onSend" which will be fired on every Send event in all nodes in all flows (if my understanding of the documentation is right) .

There _is_ an example in the documentation that shows how to use these hooks:

```auto
// Example synchronous onSend hook
RED.hooks.add("onSend", (sendEvents) => {
    console.log(`Sending ${sendEvents.length} messages`);
});

```

Keep in mind that this happens on the server - not in the client.  
Shouldn't this be what you're looking for?  
If it is not: What do you try to achieve?

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [8 May 2023 19:22 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/5 "2023-05-08T19:22:57Z")

</div>

Hi ! Thanks for your support. This example describes for me how this hook can be added (and I've tried several of these examples). But what's not clear for me is WHERE to place this "RED.hooks.add" ? I've done it in settings.js (but probably in the wrong section) and so (surely after restarting the server) I'm controlling the Server-Console - but nothing happens.

I would like to log special data of the flows-messages in an a additional log which is not loggable by the standard "node-red" logging options

Probably an additional relevant information : I'm running currently server and client on a WIN10 Environment.

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [9 May 2023 06:14 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/6 "2023-05-09T06:14:46Z")

</div>

> [@nru2023](#):
>
> But what's not clear for me is WHERE to place this "RED.hooks.add" ?

As @bakman2 already stated:

> [@bakman2](#):
>
> Hooks can be used in custom nodes;

... or plugins.

> [@nru2023](#):
>
> I would like to log special data of the flows-messages in an a additional log which is not loggable by the standard "node-red" logging options

This sounds like a 'problem' solved several times by the means Node-RED already provides - without the need to hook into the core of the runtime. If you provide some more details (e.g.: Which messages? Where shall those be logged to?), I'm convinced we'll be able to show you a way to go...

> [@nru2023](#):
>
> I'm running currently server and client on a WIN10 Environment.

The beauty of Node-RED lays in the fact that the platform it is running on - in 9 of 10 cases - doesn't matter at all...

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [9 May 2023 07:05 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/7 "2023-05-09T07:05:19Z")

</div>

Hi. Thanks for your reply. What I like to do are expand severals options - my description of logging was only a small example.

So what I really need is a short description where GLOBAL hooks has to be registered to use this functionality generally and not to a specific flow in a specific node.

Regards Fred

---

<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:** [9 May 2023 08:28 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/8 "2023-05-09T08:28:00Z")

</div>

> [@nru2023](#):
>
> So what I really need is a short description where GLOBAL hooks has to be registered to use this functionality generally and not to a specific flow in a specific node.

You need to create a Node-RED plugin that the runtime loads when it starts up.

A plugin is very similar to a normal node module, but it isn't required to provide any nodes for the palette.

For example, `node-red-debugger` is a plugin that uses the hooks: [GitHub - node-red/node-red-debugger: A flow debugger for Node-RED 2.x](https://github.com/node-red/node-red-debugger) (written in TypeScript, but the principle is the same).

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [10 May 2023 07:39 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/9 "2023-05-10T07:39:06Z")

</div>

Hi, Nick !

Thanks for your qualified answer. That‘s exactly what I’m searching for. Surely I need some time to completly analyze your code, but so I’ll be able to do my first tests. With the hooks.

Have a nice time and thanks once again.

Kind Regards

Fred

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [14 May 2023 12:27 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/10 "2023-05-14T12:27:20Z")

</div>

Hi, Nick,

I’ve done as you proposed and everything oft he examples (also with modifications) runs excellent.

But if I expand one of them for using sqlite3 module I’ll get errors.

First way : using „require(‚sqlite3‘)“ produces „cannot find module sqlite3“ (sqlite has been installed with npm with and without –g option)

Second way : configuring inside settings.js in the functionGlobalContext as

„sqlite3:require('sqlite3'),“ and using „var sqlite3=global.get('sqlite3');“ with the error „TypeError: global.get is not a function“.

I’ve also tried to activate „externalModules“ with „autoInstall: true“ and „modules: {allowInstall: true,} – no change at all.

Any idea where my mistake may be ?

Kind regards

Fred

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [14 May 2023 20:41 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/11 "2023-05-14T20:41:21Z")

</div>

> [@nru2023](#):
>
> First way : using „require(‚sqlite3‘)“ produces „cannot find module sqlite3“ (sqlite has been installed with npm with and without –g option)

1. did it install without error?
2. what were the exact `npm` commands you used?

> [@nru2023](#):
>
> and using „var sqlite3=global.get('sqlite3');“ with the error „TypeError: global.get is not a function“.

1. Where was this code written? in a function node? in a custom node/plugin?

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [15 May 2023 04:54 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/12 "2023-05-15T04:54:46Z")

</div>

Hi,

I think I found a solution last night. It seems that there is an mistake in Node-Red.

The source of my node was placed in an directory on drive „d:......\nodexyz“ The „npm install d:.....“ works fine, but when the node is processed the resolution of the „require(…);“ can not be done and result in the error. After uninstalling the node, moving the subdir of the node to „c:\Users\Name.node-red...“, reinstall it and restart the server was the solution. Right now all modules are loaded and everything works fine. Seems that the sources of nodes can not be located on an other drive !

Thanks for your interest and try to help. Kind Regards.

---

<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:** [15 May 2023 10:29 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/13 "2023-05-15T10:29:54Z")

</div>

Have you added sqlite3 to your plugins own package.json file?

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [15 May 2023 10:57 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/14 "2023-05-15T10:57:44Z")

</div>

Yes and no – I’ve tried both – with same error.

I’ve also checked if sqlite3 has been installed in the node-modules tree.

Everythings seems as is should.

The only way to find the solution was moving the directory of the node tot he same drive/.node-red path.

I think it depends to the relative pathes in the loader. (I don’t remember which .js-file it was – due to dozens of tests)

---

<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:** [15 May 2023 12:24 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/15 "2023-05-15T12:24:41Z")

</div>

> [@nru2023](#):
>
> The source of my node was placed in an directory on drive „d:......\nodexyz“

I believe that you also need to go into that directory and run  
`npm install`  
to install the dependencies.

---

<div class="post-metadata">

**Author:** ![nru2023](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@nru2023](https://discourse.nodered.org/u/nru2023)\
**Post date:** [16 May 2023 10:06 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/16 "2023-05-16T10:06:44Z")

</div>

I've did it - but it doesn't help. Thx.

---

<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:** [30 May 2023 10:07 UTC](https://discourse.nodered.org/t/how-to-activate-the-global-hooks/78251/17 "2023-05-30T10:07:39Z")

</div>

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