# Programmatically set functionGlobalContext

**URL:** <https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437>\
**Category:** General\
**Created:** [26 August 2024 07:20 UTC](https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437 "2024-08-26T07:20:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigbean17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigbean17/32/89814_2.png) [@bigbean17](https://discourse.nodered.org/u/bigbean17)\
**Post date:** [26 August 2024 07:20 UTC](https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437/1 "2024-08-26T07:20:24Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [26 August 2024 07:38 UTC](https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437/2 "2024-08-26T07:38:25Z")

</div>

See [the documentation](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option) 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](https://flows.nodered.org/node/node-red-contrib-fs-ops)

---

<div class="post-metadata">

**Author:** ![bigbean17](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bigbean17/32/89814_2.png) [@bigbean17](https://discourse.nodered.org/u/bigbean17)\
**Post date:** [26 August 2024 08:02 UTC](https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437/3 "2024-08-26T08:02:10Z")

</div>

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!

---

<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:** [9 September 2024 08:02 UTC](https://discourse.nodered.org/t/programmatically-set-functionglobalcontext/90437/4 "2024-09-09T08:02:14Z")

</div>

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