"Reference error: require is not defined (line1,col 13)

var redis = require('redis');var client = redis.createClient(6379,'localhost'); //creates a new client
client.on('connect', function() {
console.log('connected');
});
client.hget("Hash","key", function(err, reply) {
console.log(reply);
});

This is program we are running in the node-red function node but we are getting error i.e.,
"Reference error: require is not defined (line1,col 13)
please help me out with this.

Hi @akhtar321h

please read the documentation for the Function node - https://nodered.org/docs/user-guide/writing-functions

You cannot use require directly in a function, but the docs do explain how to load additional modules.

var redis = context.global.get('redis');
var client = redis.createClient(6379,'localhost'); //creates a new client
client.on('connect', function() {
    console.log('connected');
});
client.hget("hash","key", function(err, reply) {
    console.log(reply);
});```

I'm using this code now, I have debugged that require issue but now I'm getting different error i.e.,
"Type Error: Cannot read property 'create Client' of "undefined"

Did you also add the redis module the functionGlobalContext in your settings.js as described in the docs?

You also have to restart the Node-RED runtime after that.

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