JS Dependency Management
It is common for JavaScript applications to depend on libraries, or JavasScript files to depend on other files. The way we usually deal with this problem is by manually including the files we depend on on our document:
1
2
3
4
5
6
<script src="jquery.js"></script>
<script>
$(function() {
// Do something
});
</script>
To deal with this problem some people came out with the Asynchronous Module Definition (AMD) API, which allows us to specify modules or files that our code depends on and have them automatically loaded for us. The API defines one global function require that allows you to define the dependencies of the module and it’s functionality. Something like this:
1
2
3
4
5
6
7
<script>
require(['jquery'], function($) {
$(function() {
// Do something
});
});
</script>