Custom Modules Frequently Asked Questions
When should I use require versus define?
Always use the define Object for entry point scripts or when creating new modules. Use the require Function to load and use existing modules.
There’s a performance advantage to using the require Function. Whenever possible, try to use require. The define Object imports all dependencies up front, while require only loads them as needed.
Here is a summary of when to use or not use the define Object and require Function:
Object/Function |
When to use |
Caveat |
define Object |
|
If you use define, you won’t get the performance boost of progressive loading. |
require Function |
|
If you use require, you might need to import define first to get the right context. For example, to import a custom module by relative path:
|
Are third-party libraries supported?
Yes—you can define the library as a custom module, set a global identifier to reference it, and set it up as a dependency. For more examples of importing third-party and non-AMD libraries, see Import a third-party library and Import a third-party JavaScript library.