I have a two Javascript files both having some functions. I want to access functions from one file into another. How can I do it without webpack or any other module bundler.

2 Answers

0
Molina12 On

In the file (methods.js) you have the method you want to export:

    export function Example() {
        return “value”
    }

Then in the file where you will import the method:

    import Example from ‘./methods.js’;
0
Dipak Aher On

Javascript function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.

A function cannot be called unless it is in the same or greater scope, then the one trying to call it.

Suppose you declare a function function1() in One.js, and then in Two.js you can just have function1();

One.js

function function1 () {
    alert('Test');
}

Two.js

function1 ();

Test.html

<script type="text/javascript" src="One.js"></script>
<script type="text/javascript" src="Two.js"></script>