Call js file from template node

Before this worked but stopped after an update:
< script type="text/javascript" src="/ui/nameOfFile.js">
and i put the file in the .node-red directory.
Now it only works if i put the script directly in the template.

Node-RED has a feature in settings.js for defining a folder that lets you serve up files like this.

    /** When httpAdminRoot is used to move the UI to a different root path, the
     *  following property can be used to identify a directory of static content
     *  that should be served at http://localhost:1880/.
    httpStatic: process.env.httpStatic || path.join('.', 'public'),

You shouldn't be able to serve up files from your .node-red folder as that would be a big security problem if you ever ended up linking Node-RED to the Internet.

Ok thanks for the help. I tried that line but it broke the ui. so i tried unccommenting out the line that was already there httpStatic: '/home/nol/node-red-static/', but the pi wont let me create that folder so i changed it to httpStatic: '/home/pi/public/', and put the file there but still not found. It DOES work if i put the file in the node_modules/node-red-dashboard/dist folder

For my code to work, you also have to require the path standard library. You will need to create ~/.node-red/public. Of course, you could simply put that folder name in instead but my code allows you to use an environment variable to set the folder or fall back to a cross-platform reference to ./public.

Only until you do an upgrade of the Dashboard. Not a good idea to make local changes in the node_modules folders.

when i use httpStatic: process.env.httpStatic || path.join('.', 'public'), I get errors

Error loading settings file: /home/pi/.node-red/settings.js
ReferenceError: path is not defined
at Object. (/home/pi/.node-red/settings.js:107:40)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/usr/lib/node_modules/node-red/red.js:136:20)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
nodered.service: Succeeded.

You haven't added the reference to the path library.

Somewhere at the top of the settings.js file:

const path = require('path')   // Node core library. path library for cross-platform file system specs

Ok thanks for the help. still got errors with that line. But I got it working with httpStatic: '/home/pi/.node-red/public/', seems it has to point to a directory within .node-red.

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