I am using ES6 on the server with NodeJS. I use babel to transpile it and everything works correctly, but I have a problem where I do not know how to proceed. I need to be able to write this fragment of code ES5 in code ES6, I would appreciate someone explaining how to do it and why it does not work what I am doing.
In particular, I need to make this code in ES5, run in ES6:
const mailgun = require('mailgun-js')({
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
})
Currently, I am trying to use the following code, but it does not work:
import mailgun from 'mailgun-js'
mailgun = {
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
}
The configuration parameters have a config.js file which works correctly. When transpiling the code, the console returns me the following error message in the log:
Log Message
I need to know what the error is in using that syntax, and what would be the correct way to do this. Thank you.
I am using ES6 on the server with NodeJS. I use babel to transpile it and everything works correctly, but I have a problem where I do not know how to proceed. I need to be able to write this fragment of code ES5 in code ES6, I would appreciate someone explaining how to do it and why it does not work what I am doing.
In particular, I need to make this code in ES5, run in ES6:
const mailgun = require('mailgun-js')({
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
})
Currently, I am trying to use the following code, but it does not work:
import mailgun from 'mailgun-js'
mailgun = {
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
}
The configuration parameters have a config.js file which works correctly. When transpiling the code, the console returns me the following error message in the log:
Log Message
I need to know what the error is in using that syntax, and what would be the correct way to do this. Thank you.
Share Improve this question asked Mar 11, 2017 at 17:12 gach3zgach3z 5413 silver badges20 bronze badges 1- Related: stackoverflow./questions/42579853/… – T.J. Crowder Commented Mar 11, 2017 at 17:23
2 Answers
Reset to default 8The previous code is doing this:
- Requiring
mailgun-js
- Calling the result as a function, passing in some config
- Taking the result of that as
mailgun
To do the same in ES6, if we assume that the function is the default export, first we import the function, then we call it:
import mailgunFactory from 'mailgun-js';
const mailgun = mailgunFactory({
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
});
You can change your ES5 to this:
const mailgun = require('mailgun-js');
const your_var = mailgun({
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
});
In ES6, this would be:
import mailgun from 'mailgun-js';
const your_var = mailgun({
apiKey: config.mail.api.api_key,
domain: config.mail.api.domain
});