0

I am webpacking my javascript

alert("Loaded out")

function showLoaded() {
    alert("Loadedin")
}

Trying to call the function showloaded from html

<!DOCTYPE html>
<html>
  <head>
      <script type="text/javascript" src="./bundle.js"></script>
  </head>
<body>
<script>showLoaded()</script>
</body>
</html>

Loaded out is being displayed by alert , but I can't seem to call the function showLoaded(). Am I missing something obvious here.

2
  • what does bundle.js contain? Commented Aug 27, 2018 at 1:02
  • doesn't webpack create a closure, to which it puts all your rendered source? Commented Aug 27, 2018 at 1:05

2 Answers 2

2

You can also expose your function this way:

alert("Loaded out");

function showLoaded() {
    alert("Loadedin");
}

window.showLoaded = showLoaded;
Sign up to request clarification or add additional context in comments.

Comments

1

Found Answer here, How to run functions that converted by webpack? . found answer here.

I had to add

output {


    libraryTarget: "this"

  }

to my webpack config for making the function visible on global (window) object.

and used export on function

export function showLoaded() {
    alert("Loadedin5")
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.