HowTo use assert

I tried to use assert. On some pages (eg.g here) I read, that it is built in into Node.js and Node-Red. But I failed to use it. I tried:

// 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);


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

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

Then your assert variable will be accessible in your code.

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

1 Like

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

Interesting I didn't know, thanks Marcus :kissing_heart:

1 Like

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


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?

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

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

and then see my post here.

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.

1 Like

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


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 :frowning:
I'm confused regarding the error, because the connection/editor work.

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.

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.

Try

sudo systemctl restart nodered

OR

sudo systemctl restart node-red

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

The first one works - also the assertion :slight_smile:

Many thanks to all.

2 Likes

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