Importing NodeJs libraries

I am using node-red 2.0.5. Node version is 14+
I am trying to import 'fs' library as I want to use readdirSync. I am following the steps mentioned in document - loading additional module

Please note, I am on windows 10. The node-red path is "\AppData\Roaming\npm\node_modules\node-red". I have updated the settings.js file -

functionGlobalContext: {
		httpModule: require('http'),
		querystring: require('querystring'),
        os:require('os'),
		fs:require('fs')
    },

From my function node, when I access

global.get('fs').readdirSync(path)

I get the error -

[function:70da9a5b7abc738c] TypeError: Cannot read property 'readdirSync' of undefined.

When I console.log(global.get('fs')), it displays undefined. I have enabled exportGlobalContextKeys and printed the keys and it displayed empty list.
I have verified that node-red is being executed from "\AppData\Roaming\npm\node_modules\node-red"

Alternatively, you can simply add a module reference in the function node setup tab...

https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option

Well, I analyzed the process in process explorer and proc mon. However I missed a small one line display on console that says -

21 Sep 15:18:45 - [info] Settings file : C:\Users\XXXX.node-red\settings.js

It turns out I was updating the wrong settings.js file all this while. Thanks @Steve-Mcl , I will try adding to the function node.

One of many reasons I always install Node-RED locally and not globally, global files are all over the place with npm/node.js, especially on Windows. My userDir folder is always a sub-folder of the Node-RED installation folder. Makes for really easy management, development and backup.

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