How to run a custom javascript script on startup of node red

Hey there,

I'm looking to customize the UI and possibly hide or inject certain element on the node-red editor. Is there a way to have a javascript file be called on startup of node-red and run in the window?

Seems like this would probably be a useful feature.

Here's how I'm starting node red (v2) right now.

const express = require("express");
const RED = require("node-red");
const path = require("path");
const jwt = require("jsonwebtoken");
const dbConfig = require("./pg-db.config");
const app = express();
const { PostgresNodeREDAdapter } = require("./PostgresNodeREDAdapter");
const logDebug = require("./logDebug");
app.enable("trust proxy");

const port = process.env.PORT || 6700;
const authSecret = process.env.TG_AUTH_SECRET;

app.use("/", express.static("public"));

console.info(`Starting up Node-RED on port ${port}`);

if (process.env.DEBUG_NODE_RED) {
  console.info(`Node-RED debug logging enabled`);
}

const server = app.listen(port, () =>
  console.info(`Node-RED server listening on port ${port}!`)
);

const userDir = path.resolve(__dirname, "../node-red-data");

const settings = {
  editorTheme: {
    theme: "midnight-red",
    codeEditor: {
      lib: "monaco",
      options: {
        theme: "vs-dark"
      }
    }
  },
  nrlint: require("./.nrlintrc.js"),
  httpAdminRoot: "/node-red-editor",
  httpNodeRoot: "/node-red-api",
  userDir,
  dbConfig,
  autoInstallModules: true,
  storageModule: new PostgresNodeREDAdapter(),
  adminAuth: {
...etc
  },
  functionGlobalContext: {
   ...etc
  } // enables global context
};

RED.init(server, settings);

app.use(settings.httpAdminRoot, RED.httpAdmin);
app.use(settings.httpNodeRoot, RED.httpNode);

// Start the runtime
RED.start();

Thanks in advance!
-Thomas

Not quite sure what you mean. That looks like js to me. So can’t you do it from there ?

That is the server js code that serves the node red app via express. What I want to do is run a js script in the node red app in the browser upon start up

Under editorTheme in your settings object, you can set page.scripts to point at a custom JavaScript file you'd like the editor to load: Configuration : Node-RED

You provide the path of the file on the local filesystem and the runtime takes care of serving it to the editor.

3 Likes

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