0%

javascript-export

Export * from …

This syntax is used to re-export all named exports from another module. It does not work for default exports.

1
2
3
4
5
6
7
// math.mjs
const add = (a, b) => a + b;
const subtract = (a, b) => a - b;
const multiply = (a, b) => a * b;

export { add, subtract };
export default multiply; // default export

File index.mj re-exports all named exports from math.mjs, in this case, add and subtract, but not multiply, since it is a default export.

1
2
// index.mjs
export * from './math.mjs';

Now you can import all named exports from index.mjs using the following syntax:

1
2
3
4
5
6
7
8
9
// main.mjs
import * as mathfunc from './index.mjs';

console.log(mathfunc.add(1, 2)); // 3
console.log(mathfunc.subtract(2, 1)); // 1

// This one does not work, will cause an error:
/// mathfunc.multiply is not a function
console.log(mathfunc.multiply(2, 3)); // 6