# Can't get functionGlobalContext to work using docker

**URL:** <https://discourse.nodered.org/t/cant-get-functionglobalcontext-to-work-using-docker/38869>\
**Category:** General\
**Created:** [9 January 2021 21:50 UTC](https://discourse.nodered.org/t/cant-get-functionglobalcontext-to-work-using-docker/38869 "2021-01-09T21:50:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nemi/32/35086_2.png) [@nemi](https://discourse.nodered.org/u/nemi)\
**Post date:** [9 January 2021 21:50 UTC](https://discourse.nodered.org/t/cant-get-functionglobalcontext-to-work-using-docker/38869/1 "2021-01-09T21:50:30Z")

</div>

Hi people,

I am trying to add the npm module strtime so that I can use it inside the function node but I am epically failing. I am also using docker, here is what I am doing :

Dockerfile :

```auto
FROM nodered/node-red:1.2.7
RUN npm install --prefix /usr/src/node-red/node_modules/node-red/ strtime
RUN npm install --prefix /usr/src/node-red/ strtime
COPY settings.js /usr/src/node-red/node_modules/node-red/settings.js

```

According to the doc :

> Modules loaded from your settings file must be installed in the same directory as the settings file. For most users that will be the default user directory - `~/.node-red` :

But as you see I installed in both places to see if at least it would work

This is how I edited the settings.js that gets copied into the docker image to expose what I need :

```auto
functionGlobalContext: {
        stime:require("strtime")
    },

```

If I run a container using that image and I log into it :

```auto
$ docker exec -it cce9f4ecba6f /bin/bash
bash-5.0$ npm list strtime
node-red-docker@1.2.7 /usr/src/node-red
+-- node-red@1.2.7
| `-- strtime@1.1.2 
`-- strtime@1.1.2 
bash-5.0$ cd node_modules/node-red
bash-5.0$ ls
CHANGELOG.md README.md lib package-lock.json red.js
LICENSE bin node_modules package.json settings.js
bash-5.0$ grep -B8 functionGlobalContext settings.js 
    // // - code : if result is false, the HTTP error status to return
    // // - reason: if result is false, the HTTP reason string to return
    //},

    // The following property can be used to seed Global Context with predefined
    // values. This allows extra node modules to be made available with the
    // Function node.
    // For example,
    // functionGlobalContext: { os:require('os') }
    // can be accessed in a function block as:
    // global.get("os")
    functionGlobalContext: {
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
        stime:require("strtime")
    },
    // `global.keys()` returns a list of all properties set in global context.
    // This allows them to be displayed in the Context Sidebar within the editor.
    // In some circumstances it is not desirable to expose them to the editor. The
    // following property can be used to hide any property set in `functionGlobalContext`

```

From this I know :

- strtime is installed
- functionGlobalContext is supposed to be requiring strtime and setting it as stime

But then in my function node if I do this :

```auto
var stime = global.get('stime');
node.log(stime.strftime);

```

I can see in the logs the following :

```auto
node-red_1 | 9 Jan 18:30:25 - [info] Started flows
node-red_1 | 9 Jan 18:30:25 - [info] [mqtt-broker:mort_mqtt] Connected to broker: mqtts://morty:8883
node-red_1 | 9 Jan 18:30:35 - [error] [function:evaluate] TypeError: Cannot read property 'strftime' of undefined

```

Pretty much global.get('stime') is returning undefined.

Also if I go into the folder for the node-red module where the settings.js file is and I use the node console, I can import the function just fine :

```auto
bash-5.0$ pwd
/usr/src/node-red/node_modules/node-red
bash-5.0$ node
> require('strtime')
{ strftime: [Function: strftime],
  strptime: [Function: strptime] }
> require('strtime').strftime
[Function: strftime]

```

I am probably doing some dumb thing here hence the module does not get exposed inside my function node, maybe someone knows what I am doing wrong ?

Thanks!

---

<div class="post-metadata">

**Author:** ![nemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nemi/32/35086_2.png) [@nemi](https://discourse.nodered.org/u/nemi)\
**Post date:** [10 January 2021 21:26 UTC](https://discourse.nodered.org/t/cant-get-functionglobalcontext-to-work-using-docker/38869/2 "2021-01-10T21:26:49Z")

</div>

Found the problem, so replying to myself. The problem is that the settings.js for docker is actually at /data/settings.js here it says so :

[https://nodered.org/docs/getting-started/docker#dockerfile-which-copies-in-local-resources](https://nodered.org/docs/getting-started/docker#dockerfile-which-copies-in-local-resources)

Changed that and now works fine. 😃  
Thanks

---

<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:** [24 January 2021 21:26 UTC](https://discourse.nodered.org/t/cant-get-functionglobalcontext-to-work-using-docker/38869/3 "2021-01-24T21:26:52Z")

</div>

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