1

I am trying to access function defined in table.js file from index.html.
table.js

    let table;

    function set_table(table) {
      this.table = table;
      console.log("function called successfully");
    }
    
    export {set_table}; 


index.html

    <script src="table.js"></script>

    <script type="text/javascript">

      set_table("test");

    </script>

I'm using npm webpack mix to compile table.js and receiving the error Uncaught ReferenceError: set_table is not defined.

Please suggest the best way to access table.js function in index.html

2
  • Have you try to put <script src="table.js" type="text/javascript"></script> instead? Commented Sep 10, 2021 at 1:30
  • yes, I tried but no luck Commented Sep 10, 2021 at 1:52

1 Answer 1

1

Module exports need to be set on window. So, just import set_table in script type="module" and then set it on window as window.set_table

    <script type="module">
          import { set_table } from "./table.js";
          window.set_table = set_table;
        </script>

See this answer https://stackoverflow.com/a/69083651/1109657

Sign up to request clarification or add additional context in comments.

3 Comments

Still getting an error Uncaught TypeError: window.set_table is not a function index.html ` <script type="module"> import { set_table } from "./table.js"; window.set_table = set_table; </script> <script type="text/javascript"> set_table("test"); </script> `
Don't know what you are doing but see this demo codesandbox.io/s/…
I found the solution, I need to import by defining it inside script.js of webpack-mix file to make it accessible on DOM level.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.