utils/mathlib.js
export function add(x, y) {
return x + y;
}
export function subtract(x, y) {
return x - y;
}
main.js
import add from "./../utils/mathlib"; //not working. but if I do default export like `export default function add(x, y)` it will work
import { add } from "./../utils/mathlib"; //working
import * as MathLib from "./../utils/mathlib"; //working
But I want to import all the functions available in the module with the same identifier without importing separately or through importing object. Something like the below,
import * from "./../utils/mathlib"
I should be able to use add, subtract function.
The reasoning behind this use case is, Whenever I add new functions in MathLib.js it should be available without modifying.(I took MathLib as sample use case only, in my real use case all the functions is necessary whenever I import the module).
utils/mathlib.js
export function add(x, y) {
return x + y;
}
export function subtract(x, y) {
return x - y;
}
main.js
import add from "./../utils/mathlib"; //not working. but if I do default export like `export default function add(x, y)` it will work
import { add } from "./../utils/mathlib"; //working
import * as MathLib from "./../utils/mathlib"; //working
But I want to import all the functions available in the module with the same identifier without importing separately or through importing object. Something like the below,
import * from "./../utils/mathlib"
I should be able to use add, subtract function.
The reasoning behind this use case is, Whenever I add new functions in MathLib.js it should be available without modifying.(I took MathLib as sample use case only, in my real use case all the functions is necessary whenever I import the module).
Share Improve this question asked Apr 9, 2018 at 7:22 Ember FreakEmber Freak 12.9k5 gold badges26 silver badges55 bronze badges2 Answers
Reset to default 2You need to export your functions in an default object
export default {
add(x, y) {
return x + y;
}
subtract(x, y) {
return x - y;
}
}
You can't currently import into the global namespace without explicit named import/exports. It helps to prevent global namespace pollution and accidental overriding of variables in the global scope.