Can't get functionGlobalContext to work using docker

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 :

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 :

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

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

$ 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 :

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

I can see in the logs the following :

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 :

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!

1 Like

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

Changed that and now works fine. :smiley:
Thanks

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