I have a module named test1.js which is being exported default as test1.
JavaScript
x
11
11
1
//test1.js
2
3
function fun1(){
4
console.log("this is test function 1");
5
}
6
7
function fun2(){
8
console.log("this is test function 2");
9
}
10
11
export default 'test1';
Then another file is there named mod1.js importing test1.js as follwing-
JavaScript
1
2
1
import test1 from './test1.js';
2
test1.fun1();
I tried to access the function of test1.js module using the ‘.’ which is not correct. I don’t know how to access this kind of functions, even is it possible or not?
Advertisement
Answer
Your syntax of exporting is wrong. You can use curly braces {}
to export:
JavaScript
1
10
10
1
function fun1(){
2
console.log("this is test function 1");
3
}
4
5
function fun2(){
6
console.log("this is test function 2");
7
}
8
9
export {fun1, fun2};
10
Similarly you can import the same way but you need to add on the file name:
JavaScript
1
4
1
import {fun1, fun2} from 'test1.js';
2
fun1();
3
fun2();
4
For Reference: https://javascript.info/import-export