Is it okay to use global Javascript functions?

Hi @michaelblight - in the next release we’re adding the ability to save the contents of context between restarts of Node-RED. If you enable this feature, it will only work for types that can be converted to JSON - which Functions cannot be. If you don’t enable the feature then everything is stored as-is in memory and it’ll continue to work.

So what you are doing is okay - unless you want to make use of persistent context in the future. If that’s the case, then there are a couple different approaches you could use.

  1. use functionGlobalContext in your settings file to pre-seed global context with your Functions. That saves you from having to inject them every time.

  2. or, use the older syntax context.global.foo = function() { ... } rather than the get/set functions. This will ‘attach’ your function to the global context object without it going into the persistence code.

2 Likes