Question about ES6 import/export

Asked

Viewed 61 times

1

Why does the code below not work? I am using the import/export in the standard ES6 in a Node.js environment with sucrase.

Filing cabinet config.js:

const token = 'example';
export default { token };

Any other file:

import { token } from './config';

1 answer

1


The syntax { token } of a import is not to dismantling of an object. Therefore, you must export the value token thus:

export const token = 'example';

And import it so:

import { token } from './my-module';
  • Using the syntax you passed, the config.js file would have several "export const" commands. I don’t know if this is a legibility problem, but there is some way to export several constants in a single command and that can be recovered later as if it were a breakdown?

  • With the current module syntax of ES6, I think not... I will check later and as it is, edit my answer.

  • I was able to do it with "export {valor1, valor2}". I had tested before, but Eslint was accusing style error and I thought it was code problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.