functionGlobalContext: require with @

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:

"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

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.

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

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

in your Function node. This is because you are setting the sgMailglobal context value.

2 Likes

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

Worked. Thank you very much.

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