Any way to require or include a shared .js file in my nodes html definition?

390 views
Skip to first unread message

René Köcher

unread,
Dec 16, 2015, 11:16:12 AM12/16/15
to Node-RED
Hi,
so I've started working on a litte extension project for Node-RED.
So far I made good progress and came to a point where I have some common shared data in a separate file which my node code can require().
The package layout looks something like this (simplified):

 |- my-node.html
 |- my-node.js
 |- lib
 | \- shared-stuff.js
 \- package.json

Is there any way to require or include 'shared-stuff.js' from the a script-block inside 'my-node.html' ?
What I like to do is use part of the shared data in the 'oneditprepare' callback to populate a 'select' with possible choices.
(And in that case I really don't want to duplicate the data in both html & js!)

So far I tried tried something like this in my callback:

 var shared = require('lib/sharde-stuff');

event with different versions of specifying the path and with and without '.js' extension
all gave the same result: an error message from require that it can't load / locate the file.

In a desperate attempt I event tried using a separate <script type="text/javascript" src="..."> which in hindsight couldn't work..

Any help on this would be great..

Regards,
René

Dave C-J

unread,
Dec 16, 2015, 11:24:57 AM12/16/15
to node...@googlegroups.com
Hi,

see the technique used in Ben's GeoFence node - https://github.com/hardillb/node-red-node-geofence
Basically an Http request to load it into the browser when required....

René Köcher

unread,
Dec 16, 2015, 12:01:27 PM12/16/15
to Node-RED
Thanks a lot, that is pretty neat - I wouldn't have thought about doing it that way!

Sir, you're awesome.

Dave C-J

unread,
Dec 16, 2015, 1:08:55 PM12/16/15
to node...@googlegroups.com

The kudos go to Ben !

Maxim Kolchin

unread,
Jan 20, 2016, 12:01:50 PM1/20/16
to Node-RED
Hi Rene, Dave,

Could you explain, how to determine the path to a .js file, e.g `geofence/js/leaflet/leaflet-src.js`? It's located in `static` directory, but the path is different, i.e. geofence/js/leaflet/leaflet-src.js.

My environment: Node v0.13.0


Thank you in advance!
Maxim

René Köcher

unread,
Jan 20, 2016, 12:16:50 PM1/20/16
to Node-RED
Hi Maxim,
it's pretty easy (OK, Ben's version can be challenging to read..).
Take a look at my implementation which basically does the same (it returns a file from $node-install-path/lib/):


Cheers,
René

Maxim Kolchin

unread,
Jan 21, 2016, 2:12:25 AM1/21/16
to node...@googlegroups.com
Oh, okay, I missed it :) For some reason, I thought Node-RED somehow
serves such static content by itself.

Thank you very much René!

Maxim
> --
> http://nodered.org
> ---
> You received this message because you are subscribed to a topic in the
> Google Groups "Node-RED" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/node-red/MFQKemDe-l4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> node-red+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages