Skip to content
Advertisement

How to use the functions of the imported module in current module?

I have a module named test1.js which is being exported default as test1.

//test1.js

function fun1(){
    console.log("this is test function 1");
}

function fun2(){
    console.log("this is test function 2");
}

export default 'test1';

Then another file is there named mod1.js importing test1.js as follwing-

import test1 from './test1.js';
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:

function fun1(){
    console.log("this is test function 1");
}

function fun2(){
    console.log("this is test function 2");
}

export {fun1, fun2};

Similarly you can import the same way but you need to add on the file name:

import {fun1, fun2} from 'test1.js';
fun1();
fun2();

For Reference: https://javascript.info/import-export

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement