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().
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.
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());
});
...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").