When I try to import the mail service, I have the following error:
mail_1.MailService.setApiKey is not a function
import {MailService} from '@sendgrid/mail';
MailService.setApiKey('myKey');
"@sendgrid/mail": "^6.3.1" in package.jsonAny help or pointers would be great. Trying to use require() as documented gave me the error and new MailService() would not work either. Thanks!
I don't believe NodeJS (maybe in v11 it does) supports ES6 import style module imports, but rather CommonJS. But sounds like you may have tried CommonJS require as well. Also, I don't think we've tested under Node v11. The current supported environments are for Node 6, 7, 8.
Have you tried the example in our getting started?
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: '[email protected]',
from: '[email protected]',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail.send(msg);
EDIT: I just validated that the example I included above works in Node v11.
@atellier As far as I'm aware, this package is a NodeJS only one so any ES6+ import ... statement wouldn't be valid due to the fact that NodeJS modules export differently than ES ones.
And the current Node version (read v11) doesn't support ESNext's (probably ES10+ since it's already 2019) import() (which is only in stage 3).
And the example @aroach gave works well on Node 11 (I can provide evidence if needed).
As far as it's more like a speculation on my part, Node 12 (which will be released the 23rd April 2019, so around the same month where Node 6 will reach its end of life) will only support as far as ES9 went and perhaps some ES10 features (using the --harmony flag) but for now, import isn't in the stage where it's likely to be released in ES10.
Hello @atellier,
Please feel free to re-open if you still need some help.
With Best Regards,
Elmer
I replaced
import {MailService} from '@sendgrid/mail';
with
import MailService from '@sendgrid/mail';
and everything worked as expected.
The typescript types are not kept up to date and it uses module.exports so to use the types:
"@sendgrid/mail": "^6.4.0",
import MailService from "@sendgrid/mail/src/mail";