Ata Sanchez Ata Sanchez - 9 months ago 30
Javascript Question

Import ES6 modules with properties

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.

Answer Source

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
});