# Importing NodeJs libraries

**URL:** <https://discourse.nodered.org/t/importing-nodejs-libraries/51357>\
**Category:** General\
**Created:** [21 September 2021 19:14 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357 "2021-09-21T19:14:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tusharacc](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tusharacc](https://discourse.nodered.org/u/tusharacc)\
**Post date:** [21 September 2021 19:14 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357/1 "2021-09-21T19:14:27Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions#loading-additional-modules)

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 -

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

```

From my function node, when I access

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 September 2021 19:20 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357/2 "2021-09-21T19:20:11Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions#using-the-functionexternalmodules-option)

---

<div class="post-metadata">

**Author:** ![tusharacc](https://avatars.discourse-cdn.com/v4/letter/t/74df32/32.png) [@tusharacc](https://discourse.nodered.org/u/tusharacc)\
**Post date:** [21 September 2021 19:25 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357/3 "2021-09-21T19:25:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 September 2021 20:06 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357/4 "2021-09-21T20:06:29Z")

</div>

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.

---

<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:** [5 October 2021 20:07 UTC](https://discourse.nodered.org/t/importing-nodejs-libraries/51357/5 "2021-10-05T20:07:02Z")

</div>

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