# How to use Nodejs modules within a template

**URL:** <https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090>\
**Category:** FAQ\
**Tags:** template\
**Created:** [January 21, 2025, 3:50pm UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090 "2025-01-21T15:50:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrankLeggett](https://casparcgforum.org/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@FrankLeggett](https://casparcgforum.org/u/FrankLeggett)\
**Post date:** [January 21, 2025, 3:50pm UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/1 "2025-01-21T15:50:19Z")

</div>

Hi,

I am trying to use the various Nodejs modules inside my template without success.

For example I would like to use the “FS” (FileSystem) to update my template when it see a data file has been updated.

I have attempted to use “requireJS” as a min.js reference in order to get the module to work but the error back is: “uncaught referenceerror: require is not defined”.

Could some help by posting a few lines on how to use FS inside a template?

Many thanks  
Frank

---

<div class="post-metadata">

**Author:** ![Nytamin](https://casparcgforum.org/user_avatar/casparcgforum.org/nytamin/32/10_2.png) [@Nytamin](https://casparcgforum.org/u/Nytamin)\
**Post date:** [January 21, 2025, 4:34pm UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/2 "2025-01-21T16:34:47Z")

</div>

The nodejs api (like fs) aren’t available in the browser, you’ll have to resort to web api instead. If you need local file access, the File System Api ([File System API - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/File_System_API)) might work, I’m a tad unsure whether the chromium renderer in CasparCG allows that though…

Another way you could achieve what you want, is to run a separate nodejs application that monitors your files, and then you set up a websocket connection between your template and nodejs, and send the data over that.

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [January 21, 2025, 6:46pm UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/3 "2025-01-21T18:46:23Z")

</div>

What sure works is local storage.

---

<div class="post-metadata">

**Author:** ![FrankLeggett](https://casparcgforum.org/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@FrankLeggett](https://casparcgforum.org/u/FrankLeggett)\
**Post date:** [January 22, 2025, 9:30am UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/4 "2025-01-22T09:30:53Z")

</div>

Hi didkunz, thankyou for your post, can you explain what you mean by “What sure works is local storage”. I apologise for not understanding the advice.

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [January 22, 2025, 9:47am UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/5 "2025-01-22T09:47:06Z")

</div>

Oh, sorry. There is a thing in JavaScript, that allows you to store key/value pairs. They will be presided over sessions. This thing is called LocalStorage.

But for your application that is maybe not useful. It is possible to read the content of a file (text, XML or JSON) from inside a template, by using `XMLHttpRequest`. But AFAIK it is not possible to read the last-updated date of a file. So you would need too pool for changes. For `XMLHttpRequest` to work you need to start CasparCG Server with a command similar to this:  
`casparcg.exe casparcg.config --disable-web-security`

What exactly is the use case? What is inside the these data-files? And have you control over the content of them?

---

<div class="post-metadata">

**Author:** ![FrankLeggett](https://casparcgforum.org/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@FrankLeggett](https://casparcgforum.org/u/FrankLeggett)\
**Post date:** [January 22, 2025, 10:52am UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/6 "2025-01-22T10:52:19Z")

</div>

We are taking data from a text file and populating a display with the content. The data is for auctions so it changes everytime there is a bid or the lot is sold and there is new lot. At the moment we are polling the text file every second with a loop function but we noticed that NodeJS has a Watchfile module which would be a lot simplier but we couldn’t get it to work inside the template.  
We are currently looking at running a small c# programme that will listen and then hopefully update the template via a websocket (we are now learning about websockets).  
My programmer has a c# history so is more comfortable about doing the eventlistner in C but we still would like to learn to do more with Java.

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [January 22, 2025, 11:17am UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/7 "2025-01-22T11:17:11Z")

</div>

> [@FrankLeggett](#):
>
> We are currently looking at running a small c# programme that will listen and then hopefully update the template via a websocket (we are now learning about websockets).

You can simply send a [CG UPDATE](https://github.com/CasparCG/help/wiki/AMCP-Protocol#cg-update) command to the template from your C# program. That would maybe be much easier, than doing it with WebSocket.

---

<div class="post-metadata">

**Author:** ![noquierouser](https://casparcgforum.org/user_avatar/casparcgforum.org/noquierouser/32/155_2.png) [@noquierouser](https://casparcgforum.org/u/noquierouser)\
**Post date:** [January 29, 2025, 11:45am UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/8 "2025-01-29T11:45:22Z")

</div>

I see your use case, but [understanding the security model of browsers](https://html.spec.whatwg.org/multipage/input.html#dom-input-value-filename), what you want to achieve won’t be possible. Now, [there’s a recently available API](https://developer.chrome.com/docs/capabilities/web-apis/file-system-access) that might be closer to your use case, but I can’t exactly tell if it is supported by the Chrome Embedded Framework version that CasparCG is using.

Now, as has been mentioned, you could use WebSockets or send `CG UPDATE` using an external program. Personally, I’d incline for WebSockets, as it can easily provide a space for more integrations, and there are plenty of libraries that will allow you to implement those. Also, it is a proven solution, as many of the most modern CG controllers do effectively use an implementation of WebSockets, like [socket.io](https://socket.io), that are compatible with CEF versions included in CasparCG as far back as CasparCG 2.2.

I have used [Laravel Reverb](https://reverb.laravel.com) lately, as I’m a PHP developer mainly, and it has worked fine in the applications I have developed. I’ve also done long-polling applications using Laravel that have worked just fine.

---

<div class="post-metadata">

**Author:** ![FrankLeggett](https://casparcgforum.org/letter_avatar_proxy/v4/letter/f/d9b06d/32.png) [@FrankLeggett](https://casparcgforum.org/u/FrankLeggett)\
**Post date:** [January 29, 2025, 12:25pm UTC](https://casparcgforum.org/t/how-to-use-nodejs-modules-within-a-template/7090/9 "2025-01-29T12:25:44Z")

</div>

Thanks for this response, we are trying via the CG Update command first and then try other options.
