# HowTo use assert

**URL:** <https://discourse.nodered.org/t/howto-use-assert/77718>\
**Category:** General\
**Created:** [19 April 2023 16:10 UTC](https://discourse.nodered.org/t/howto-use-assert/77718 "2023-04-19T16:10:13Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [19 April 2023 16:10 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/1 "2023-04-19T16:10:13Z")

</div>

I tried to use assert. On some pages (eg.g [here](https://www.npmjs.com/package/assert)) I read, that it is built in into Node.js and Node-Red. But I failed to use it. I tried:

```auto
// Testskript für den Flow
const assert = require("assert");

// Importieren der assert-Bibliothek
//const assert = global.get("assert");
/*
const assert = (function () {
    const module = require("module");
    const assert = require("assert");
    return assert.deepStrictEqual ? assert : new module.Module()._compile("const assert = require('assert'); assert.deepStrictEqual = assert.deepEqual; assert;", "assert.js");
})();
*/ 

and later in my code I want to use it:

```

assert.deepStrictEqual(activeConsumers, expectedOutput);

```auto

I always get an error. So what to do to use assertion?

```

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [19 April 2023 16:21 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/2 "2023-04-19T16:21:26Z")

</div>

Hi,  
Use a `function` node and configure the setup tab like this:

 ![Capture d’écran 2023-04-19 à 18.17.11](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a0293c8927391c6b1baf85fa77d996fbf227181.png)

Then your `assert` variable will be accessible in your code.

Don't forget to install `assert` module in `.node-red` folder first.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [19 April 2023 16:25 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/3 "2023-04-19T16:25:33Z")

</div>

> [@GogoVega](#):
>
> Don't forget to install `assert` module in `.node-red` folder first.

If adding the required module in the function node properties, you don't need to install the required module manually.

Node RED will pull it in automatically for you.

Also @Moosbueffel you can't use `require` in a function node.  
Just call into any offerings by the `import as` value

so if `import as` is set to `AST` then In a function node:

`AST.deepStrictEqual`

---

<div class="post-metadata">

**Author:** ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)\
**Post date:** [19 April 2023 16:27 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/4 "2023-04-19T16:27:44Z")

</div>

Interesting I didn't know, thanks Marcus 😘

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [19 April 2023 16:28 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/5 "2023-04-19T16:28:45Z")

</div>

Thanks @GogoVega, but I think I'm blind. My Setup-Tab of my function node looks like this:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/a/4a2f78eaac9eed7736e4e9bf195cbf74bc5b4b75.png)  
There is no possibility to import modules.

But perhaps the main reason is that assert module isn't installed in my .node-red folder. I assume I must have than a folder /home/pi/.node-red/node\_modules/assert ?

How can II install assert as a module? And does I need to extend the settings.js part: functionGlobalContext?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [19 April 2023 16:32 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/6 "2023-04-19T16:32:19Z")

</div>

You need to enable module support in function nodes in the Node Red Settings file `settings.js`

```auto
/** Allow the Function node to load additional npm modules directly */
functionExternalModules: true,

```

and then see my post here.

> [@HowTo use assert](https://discourse.nodered.org/t/howto-use-assert/77718/3):
>
> If adding the required module in the function node properties, you don't need to install the required module manually. Node RED will pull it in automatically for you. Also @Moosbueffel you can't use require in a function node. Just call into any offerings by the import as value so if import as is set to AST then In a function node: AST.deepStrictEqual

---

<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:** [19 April 2023 16:38 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/7 "2023-04-19T16:38:32Z")

</div>

`assert` is a built-in module of node.js - you don't need to install it separately. But you do still need to add it to the external modules list in the Function node - the runtime will recognise its a built-in module and not try to install it from npm.

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [19 April 2023 16:48 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/8 "2023-04-19T16:48:41Z")

</div>

Thanks, I changed my settings.js and did a node-red restart, getting the following output:

```auto

Welcome to Node-RED
===================

19 Apr 18:38:21 - [info] Node-RED version: v3.0.2
19 Apr 18:38:21 - [info] Node.js version: v16.19.1
19 Apr 18:38:21 - [info] Linux 5.10.103-v7+ arm LE
19 Apr 18:38:23 - [info] Loading palette nodes
19 Apr 18:38:42 - [info] Dashboard version 3.4.0 started at /ui
19 Apr 18:38:45 - [info] Settings file : /home/pi/.node-red/settings.js
19 Apr 18:38:45 - [info] Context store : 'default' [module=memory]
19 Apr 18:38:45 - [info] User directory : /home/pi/.node-red
19 Apr 18:38:45 - [warn] Projects disabled : editorTheme.projects.enabled=false
19 Apr 18:38:45 - [warn] Flows file name not set. Generating name using hostname.
19 Apr 18:38:45 - [info] Flows file : /home/pi/.node-red/flows_raspberrypi.json
19 Apr 18:38:45 - [error] Uncaught Exception:
19 Apr 18:38:45 - [error] Error: listen EADDRINUSE: address already in use 0.0.0.0:1880
    at Server.setupListenHandle [as _listen2] (node:net:1463:16)
    at listenInCluster (node:net:1511:12)
    at doListen (node:net:1660:7)
    at processTicksAndRejections (node:internal/process/task_queues:84:21)

```

Than I restarted the editor. But I always get the old setup without the possibility to nomw modules ☹  
I'm confused regarding the error, because the connection/editor work.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [19 April 2023 16:51 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/9 "2023-04-19T16:51:56Z")

</div>

How are you running Node RED?

- Interactively
- Systemd

The _currently_ running Node RED needed restarting, not a new instance being started  
Your old instance never got restarted by the sounds of it - as you can't see the changes you made to settings.

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [19 April 2023 17:08 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/10 "2023-04-19T17:08:57Z")

</div>

I did a **node-red-restart**.

I'm running with systemd (as a service?). node-red starts automatically after reboot

Sorry I'm not a very good linuxer.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [19 April 2023 17:14 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/11 "2023-04-19T17:14:28Z")

</div>

Try

```auto
sudo systemctl restart nodered

```

OR

```auto
sudo systemctl restart node-red

```

My install is not traditional so can't comment on **node-red-restart**

---

<div class="post-metadata">

**Author:** ![Moosbueffel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/moosbueffel/32/36732_2.png) [@Moosbueffel](https://discourse.nodered.org/u/Moosbueffel)\
**Post date:** [19 April 2023 17:39 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/12 "2023-04-19T17:39:07Z")

</div>

The first one works - also the assertion 🙂

Many thanks to all.

---

<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:** [3 May 2023 17:39 UTC](https://discourse.nodered.org/t/howto-use-assert/77718/13 "2023-05-03T17:39:10Z")

</div>

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