Programmatically set functionGlobalContext

Hi,

I'm trying to import a third party library to my node-red project. I could manually set functionGlobalContext and it worked. functionGlobalContext: {os: require('os)} and import it by const os = global.get('os')

But is it possible to do this programmatically? What I tried is that

const fs = require('fs');

// Path to settings file
const filePath = '/root/.node-red/settings.js';

// Read the settigns file using require
const config = require(filePath);

config.credentialSecret = "xx"
config.editorTheme.projects.enabled = true;
config.functionGlobalContext.os = "require('os')";

The problem with above code is that if I import it by global.get('os'), it will give me a string of "require('os')".

The reason why I need to do this programmatically is that I have thousands of devices needs to be setup, it is impossible to manually set functionGlobalContext.

Any help would be appreciated.

Thank you!

See the documentation on how to import modules within a function node.

But are you certain you need to import 'fs' ? There are already build in nodes for files and you can install dedicated nodes for file operations, like fs-ops

Sorry for the confusion, the code snippet above is the script that setup the node-red.

So it will read the existing "settings.js" and set the value such as "credentialSecret", then write the edited config back to "settings.js"

I will have a try with your solution, thanks for helping!

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