ES6 export and import features?

I have some functions and I want to save them in an external js file. eg.

in functions.js

var double = function(x) {
  return x + x;
}

export { double };

Then in my main js file:

import double from './functions';

...

double(2)

I get this error:

Uncaught TypeError: (0 , c.default) is not a function
    at bundle.min.js:44

When I read line 44:

(0, _functions2.default)(2);

Any ideas why? What did I miss?

+4
source share
2 answers

You are misleading by default and call export.

If you export { double }, you mustimport { double } from './functions';

If you have only one export, it is preferable to use the default export:

export default double;

Then you can import double from './functions':

The reason for this is that named export allows you to import only part of a module. For instance:

export function add (a, b) { return a + b; }
export function subtract (a, b) { return a - b; }

Then you can import { add } from './math.js';not import subtract.

, , .

+8

:

var double = function(x) {
  return x + x;
}

 export {double};

import {double} from './functions';

double;

double "./functions.js";

+2

Source: https://habr.com/ru/post/1683009/


All Articles