TypeError: Cannot read property 'nodes' of undefined

Hello, I have two javascript-files for some custom-nodes...
Goal is to use the functionality from "hello-world.js" which is a custom-node in the "test.js".

In the first one "hello-world.js" - I'm writing an input from an inject node in a file called hello.txt. This happens in helloFunction().

var exports = module.exports = {};

exports.oneFunction = function(RED) {

    function helloFunction (config) {
    
        RED.nodes.createNode(this,config);
        const fs = require('fs');
        path = require('path');
        filePath = path.join(__dirname, 'hello.txt');
        var node = this;
        node.on('input', function(msg) {
            var fileContent = msg.payload +  '\n';
            fs.appendFile(filePath, fileContent , (err) => { 
                if (err) throw err });
                node.send(msg);    
        });   
    };

    RED.nodes.registerType("hello-world", helloFunction);
}

In the second one "test.js" - I'm importing the "hello-world.js".

 const helloWorld = require("./hello-world");
    
    function testing() {

        helloWorld.oneFunction();
    }

    console.log(testing());

When i execute "test.js" I get a TypeError: Cannot read property 'nodes' of undefined in the "hello-world.js".

The problem comes because your test.js file is calling helloWorld.oneFunction(); without any arguments. However, that function expects one argument to be passed in:

exports.oneFunction = function(RED) {

RED is the handle to the node-red runtime that is given to nodes when the runtime loads their module.

The error TypeError: Cannot read property 'nodes' of undefined means precisely that. The first line of helloFunction calls RED.nodes.createNode(this,config); but because you haven't passed in RED, that will be undefined.

1 Like

I tried it out but now the TypeError: this.updateWires is not a function at the file Node.js occures. Any suggestions?

Without seeing your code, no, I cannot tell you why you are hitting that error.

File: test.js

var http = require('http');
var express = require("express");
var RED = require("node-red");


// Create an Express app
var app = express();

// Add a simple route for static content served from 'public'
app.use("/",express.static("public"));

// Create a server
var server = http.createServer(app);

// Create the settings object - see default settings.js file for other options
var settings = {
    httpAdminRoot:"/red",
    httpNodeRoot: "/api",
    userDir:"/home/nol/.nodered/",
    functionGlobalContext: { }    // enables global context
};

// Initialise the runtime with a server and settings
RED.init(server,settings);


// Serve the editor UI from /red
app.use(settings.httpAdminRoot,RED.httpAdmin);

// Serve the http nodes UI from /api
app.use(settings.httpNodeRoot,RED.httpNode);


server.listen(8000);


const helloWorld = require("./hello-world");
    
function testing() {
    
    helloWorld.oneFunction(RED);
}

// Start the runtime
RED.start().then(() => {
    console.log(testing());
});

File: hello-world.js

var exports = module.exports = {};

exports.oneFunction = function(RED) {

    function helloFunction (config) {
    
        RED.nodes.createNode(this,config);
        const fs = require('fs');
        path = require('path');
        filePath = path.join(__dirname, 'hello.txt');
        var node = this;
        node.on('input', function(msg) {
            var fileContent = msg.payload +  '\n';
            fs.appendFile(filePath, fileContent , (err) => { 
                if (err) throw err });
                node.send(msg);    
        });   
    };

    RED.nodes.registerType("hello-world", helloFunction);
}

That isn't how Node-RED loads modules. You don't call it manually - you let node-red find your module on the node path and it will load it properly.

With calling the module manually you mean the require("./hello-world"); in the test.js, right?

I quite don't understand what you mean with...

...That isn't how Node-RED loads modules. You don't call it manually - you let node-red find your module on the node path and it will load it properly.

Out of curiosity, is there a reason why you don't follow the docs on creating custom nodes, and for shared code between different nodes use a separate javascript module that is imported to both?

Your code in test.js should not be trying to require and initialise your node module itself.

The Node-RED runtime is responsible for loading the nodes. The RED object that gets passed to the node module function is unique to that module - it is not the RED object you get from require("node-red").