# Use of fs as a global module (best practice )

**URL:** <https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663>\
**Category:** General\
**Created:** [4 April 2021 21:29 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663 "2021-04-04T21:29:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [4 April 2021 21:29 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/1 "2021-04-04T21:29:03Z")

</div>

I am after some advice on best/appropriate practice. I am using the fantastic node-red-contrib-google-oauth2 node to connect to the google APIs to upload files to google drive.

The v3 of the drive API requires that a ReadStream rather than a buffer be used for content upload.

I have loaded the fs module as a global module in the settings.js

```auto
  functionGlobalContext: {
        fs:require('fs'),
    },

```

and then use it it in the function node preparing the API call.

```auto
const fs = global.get('fs')

let fileMetadata = {
    name : msg.filenameShort,
}

let media = {
    mimeType : 'application/x-tar',
    body : fs.createReadStream(msg.filename)
}

msg.payload = {
    resource : fileMetadata,
    media : media,
    fields : 'id'
}

return msg

```

Is this an apprpriate use of global modules or is there a better way to achieve this ?

Thanks

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [5 April 2021 05:06 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/2 "2021-04-05T05:06:07Z")

</div>

I also use it exactly in the same way

---

<div class="post-metadata">

**Author:** ![BartButenaers](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bartbutenaers/32/10476_2.png) [@BartButenaers](https://discourse.nodered.org/u/BartButenaers)\
**Post date:** [5 April 2021 05:54 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/3 "2021-04-05T05:54:03Z")

</div>

Hi @chrisn-au,  
I think that in the next 1.3 version (which will be released soon), you won't need to manually edit the settings.js file anymore: see this [pull-request](https://github.com/node-red/node-red/pull/2873).  
Bart

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [5 April 2021 06:04 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/4 "2021-04-05T06:04:15Z")

</div>

Bart, thanks Looking forward to 1.3 especially being able to package subflows as npm modules

---

<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:** [6 April 2021 10:01 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/5 "2021-04-06T10:01:52Z")

</div>

Also worth noting that Node-RED makes extensive use of fs-extra which I also use in uibuilder since it provides some really useful features over and above the standard fs but is also fully backward compatible. What it means though is that fs-extra is already loaded so using it doesn't add any extra overheads.

---

<div class="post-metadata">

**Author:** ![chrisn-au](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chrisn-au/32/25504_2.png) [@chrisn-au](https://discourse.nodered.org/u/chrisn-au)\
**Post date:** [6 April 2021 10:10 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/6 "2021-04-06T10:10:42Z")

</div>

@TotallyInformation That is interesting thanks - How do I access fs-extra. Is it exposed as a global ?

---

<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:** [6 April 2021 11:24 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/7 "2021-04-06T11:24:48Z")

</div>

Hi, no, it is just another module so reference it in the normal way. You will need to include it in your package.json so just install in the normal fashion and let npm and node.js take care of the details. This is one of the strengths of node.js (when it gets it right, it can occasionally go spectacularly wrong for a variety of reasons but it mostly works and I've had no issues with fs-extra).

---

<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 June 2021 11:25 UTC](https://discourse.nodered.org/t/use-of-fs-as-a-global-module-best-practice/43663/8 "2021-06-05T11:25:04Z")

</div>

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