# Call js file from template node

**URL:** <https://discourse.nodered.org/t/call-js-file-from-template-node/36693>\
**Category:** Dashboard\
**Created:** [28 November 2020 21:36 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693 "2020-11-28T21:36:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianb](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@brianb](https://discourse.nodered.org/u/brianb)\
**Post date:** [28 November 2020 21:36 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/1 "2020-11-28T21:36:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 November 2020 22:41 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/2 "2020-11-28T22:41:17Z")

</div>

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

```auto
    /** 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.

---

<div class="post-metadata">

**Author:** ![brianb](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@brianb](https://discourse.nodered.org/u/brianb)\
**Post date:** [28 November 2020 23:26 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/3 "2020-11-28T23:26:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [28 November 2020 23:59 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/4 "2020-11-28T23:59:11Z")

</div>

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`.

> [@brianb](#):
>
> It DOES work if i put the file in the node\_modules/node-red-dashboard/dist folder

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

---

<div class="post-metadata">

**Author:** ![brianb](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@brianb](https://discourse.nodered.org/u/brianb)\
**Post date:** [29 November 2020 00:33 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/5 "2020-11-29T00:33:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [29 November 2020 00:35 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/6 "2020-11-29T00:35:34Z")

</div>

> [@brianb](#):
>
> ReferenceError: path is not defined

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

Somewhere at the top of the settings.js file:

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

```

---

<div class="post-metadata">

**Author:** ![brianb](https://avatars.discourse-cdn.com/v4/letter/b/5e9695/32.png) [@brianb](https://discourse.nodered.org/u/brianb)\
**Post date:** [29 November 2020 01:06 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/7 "2020-11-29T01:06:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 December 2020 01:06 UTC](https://discourse.nodered.org/t/call-js-file-from-template-node/36693/8 "2020-12-29T01:06:39Z")

</div>

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