最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Import ES6 modules with properties - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 8

The previous code is doing this:

  1. Requiring mailgun-js
  2. Calling the result as a function, passing in some config
  3. 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
});
发布评论

评论列表(0)

  1. 暂无评论