# functionGlobalContext: require with @

**URL:** <https://discourse.nodered.org/t/functionglobalcontext-require-with/33263>\
**Category:** General\
**Created:** [24 September 2020 07:48 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263 "2020-09-24T07:48:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![christl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christl/32/443_2.png) [@christl](https://discourse.nodered.org/u/christl)\
**Post date:** [24 September 2020 07:48 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/1 "2020-09-24T07:48:52Z")

</div>

I am trying to access a standalone library in a function node and the approach shown in generic nodejs examples for the require command looks like this:  
`const sgMail = require('@sendgrid/mail');`

I have added the following (including some other modules which work) to the .node-red folder in settings.js under functionGlobalContext:  
`sgMail: require('@sendgrid/mail')`

and the following at the top of my function node:  
`var sgMail = global.get("@sendgrid/mail");`

Then when this is executed:  
`sgMail.setApiKey(process.env.SENDGRID_API_KEY);`

It returns the following which I think indicates that the sendgrid module never got loaded.  
`"TypeError: Cannot read property 'setApiKey' of undefined"`

Looking for advice and the meaning of the @ character in the `require('@sendgrid/mail')` syntax.

Any help appreciated. - Thanks in advance.

**Edit** - This has also been added to the **/.node-red** folder's **package.json** file:

```auto
"dependencies": {
        "@sendgrid/mail": "^7.2.6",

```

> Node-RED version: v1.1.3  
> Node.js version: v10.22.0  
> Linux 5.4.0-1024-gcp x64 LE

---

<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:** [24 September 2020 08:19 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/2 "2020-09-24T08:19:24Z")

</div>

You can probably test that the require worked. I suspect that it is the `global.get` is what fails because of the embedded `@` symbol. If so, that is an edge case that will need handling in core. One for Nick or Dave.

I have a similar problem with uibuilder that I've not managed to fix yet.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [24 September 2020 08:26 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/3 "2020-09-24T08:26:06Z")

</div>

@christl if you have added `sgMail: require('@sendgrid/mail')` to the `functionGlobalContext` value in your settings file, then you would do:

```auto
var sgMail = global.get("sgMail");

```

in your Function node. This is because you are setting the `sgMail`global context value.

---

<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:** [24 September 2020 08:27 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/4 "2020-09-24T08:27:29Z")

</div>

Doh! Missed that complexly - blaming lack of coffee input. Will shut down and reboot myself.

---

<div class="post-metadata">

**Author:** ![christl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christl/32/443_2.png) [@christl](https://discourse.nodered.org/u/christl)\
**Post date:** [24 September 2020 08:38 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/5 "2020-09-24T08:38:35Z")

</div>

> [@knolleary](#):
>
> `var sgMail = global.get("sgMail");`

Worked. Thank you very much.

---

<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:** [8 October 2020 08:38 UTC](https://discourse.nodered.org/t/functionglobalcontext-require-with/33263/6 "2020-10-08T08:38:54Z")

</div>

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