Peter_St
Peter_St

Reputation: 61

Problems at require(<Module>) in self written Node-RED node

I added a self written WebSocket-Client library. When I require in node.js it works fine, just as in Node-RED's function-node with registering it in settings.js and requireing it by global.get("RWSjs").

Now I had to write a Node by myself and wanted to require this file, and it doesn't work. Node-RED always gives me the "node not deployed" error, which is, I think, because of a javascript syntax error.

How can I require a self written module in a self written node's .js?

Thanks a lot in advance, Peter :)

Edit:

some Code:

eval-R-char.js (Code for the node)

module.exports = function(RED) {               

    // doesn't work:
    var RWSjs = global.get("RWSjs");

    function EvalRCharNode(config) {            
        RED.nodes.createNode(this,config);      

        this.instruction = config.instruction;
        var node = this;
        this.on('input', function(msg) {        
            //msg.payload = msg.payload.toLowerCase();
            msg.payload = "Instruction: " + this.instruction;
            node.send(msg);                     
        });                                     
    }
    RED.nodes.registerType("eval-R-char",EvalRCharNode); 
}

Upvotes: 0

Views: 756

Answers (1)

hardillb
hardillb

Reputation: 59618

You shouldn't use the context to require modules when writing your own nodes, it is purely a workaround as you can't use require in the function node.

You should just require as normal in your custom node.

So in this case:

module.exports = function(RED) {               

    //assuming your module is in the RWS.js file in the same directory
    var RWSjs = require('./RWS.js');

    function EvalRCharNode(config) {            
        RED.nodes.createNode(this,config);      

        this.instruction = config.instruction;
        var node = this;
        this.on('input', function(msg) {        
            //msg.payload = msg.payload.toLowerCase();
            msg.payload = "Instruction: " + this.instruction;
            node.send(msg);                     
        });                                     
    }
    RED.nodes.registerType("eval-R-char",EvalRCharNode); 
}

Upvotes: 1

Related Questions