kubetail-org / loadjs

A tiny async loader / dependency manager for modern browsers (899 bytes)
MIT License
2.57k stars 149 forks source link

Using loadjs in a Vanilla project: require is not defined error #104

Closed MartijnHarmenzon closed 4 years ago

MartijnHarmenzon commented 4 years ago

I'm running a Vanilla JS project without any builders or frameworks. I would like to make use of loadjs but I'm getting the error:

require is not defined

My code:

  <script src="https://unpkg.com/loadjs@4.2.0/dist/loadjs.min.js"></script>
  <script>const loadjs = require('loadjs');</script>

How to include loadjs in a Vanilla project?

amorey commented 4 years ago

Since you're using the standalone browser payload, the loadjs library is available via the loadjs global:

<script src="https://unpkg.com/loadjs@4.2.0/dist/loadjs.min.js"></script>
<script>
  loadjs(["/path/to/foo.js", "/path/to/bar.js"], function() {
    /* foo.js and bar.js loaded */
  });
</script>

Hope that helps! I updated the README to make this clearer.

MartijnHarmenzon commented 4 years ago

@amorey Thanks for the quick reply and solution. I can confirm it works.