NodeRed Function Block Require

I am still an amateur with Node-Red.

I want to run this logic in a function node.

I am getting this error.

ReferenceError: require is not defined (line 2, col 13)

Searching for solutions it looks like I need to install “npm install mariadb”

Is this possible with Node-Red on an groovEpic PR1?

// Required modules
var mariadb = require(‘mariadb’);

// Define database connection configuration
var pool = mariadb.createPool({
host: ‘’,
user: ‘master’,
password: ‘password’,
database: ‘database’

// Attempt to connect to the database
.then(conn => {
console.log(‘Connected to MariaDB database’);
// If connected, release connection
.catch(err => {
console.error(‘Error connecting to MariaDB database:’, err.message);
// Set alarm variable
flow.set(‘database_alarm’, 'Error connecting to MariaDB database: ’ + err.message);

// Return message for the connecting function node
return {
payload: ‘Connected to MariaDB database’

In short, thats not how you would make a connection with mariadb in Node-RED.
Looks like its just a cut/paste from some computer code vs Node-RED.

You will need to first ensure you have a shell license installed on your EPIC. (Its a free option, but if you are not comfortable with a good level of Linux command line experience, you might want to rethink your setup).
Check out to get the outline on installing mariadb on EPIC.

Once thats up and running, you will need the mySQL node, install it via the palette manager in Node-RED.
Once that is done and configured, you can then format your data that you want to insert in the Node-RED flow. Lots of examples for that here in the forums or on our Opto22 YouTube channel.
For select statements, you would use the function node and set the msg.topic to get the data you are looking for.

1 Like

Thanks for the reply.

Yes, I have been using the mySQL node successfully for a while.

I am working on a method to monitor a connection to my MariaDB.

I need to know when it is not operational.

I have tried sending a SQL query like “SELECT @@VERSION”; periodically and using a Catch node to trap for errors.

I have 17 instances of the mySQL node…so when I go to test the solution all 17 connection failed instances are being trapped by the Catch node.

And since the mySQL node connection is configured in the node the “SELECT @@VERSION” query doesn’t work because the connection failed.

Is the catch node indicating that the connection has failed not sufficient to know when the DB is not up? Is there something else you’re looking for?

Also, beyond just checking the status, do you know what is causing the connection loss? Is it server side? Network related? Are the nodes being overloaded with statements?
You could reduce the 17 connections by using link nodes or otherwise reworking your flow.

I couldn’t get the catch node to only look at the 1 node-red-node-mysql node. The catch node would overflow with errors because it was catching all 17.

I was able to make it work by adding a different type of MySQL node (node-red-contrib-stackhero-mysql)…adding the catch node with a function node to parse the error and email it.

This is a preventive measure. Our customer’s operation comes with large penalties for downtime. We want to know if we have a database issue as soon as possible.

To test, I was stopping the MariaDB using an exec node to issue sudo /etc/init.d/mysqld stop .

For my edification…I have used the code in my first post on Node-Red instances running on a computer. Is it not possible to load node package managers in the groovEpic PR1 Node Red …and make connections via function blocks?


I think you have to name all the nodes different, and then the catch node can be set to catch each one by its name.
17 connections to the same DB on a remote PC sounds odd. I suspect we are not really understanding your setup very well.

Sorry that your first post gave me the impression that the mariadb was installed on the EPIC.

Sounds like your Windows PC that you ran the initial Node-RED code on also had mySQL drivers on it, so that explains why that worked.

Yes, EPIC (really has nothing to do with EPIC, its more a remote Node-RED question) could be made to work much the same, if I am understanding your system build correctly… mySQL on a remote PC and Node-RED on an EPIC at another location. Depends on your network setup to some extent. ie, remote db connections.