JavaScript ordering in asset libraries in Drupal
Good question — the answer is yes, generally theme JS runs after module JS, but it's not simply "modules before themes" as a hard rule. Here's how it actually works:
The Primary Driver: Library Groups
The group constant is the biggest factor:
Group Constant | Value | Typical Owner |
|---|---|---|
| -100 | Core/contrib low-level libs |
| 0 | Modules |
| 100 | Themes |
Drupal's asset pipeline sorts by group first, so anything in JS_THEME (100) outputs after anything in JS_DEFAULT (0) regardless of when it was attached.
How Groups Get Assigned
In a *.libraries.yml file:
# Module — defaults to JS_DEFAULT (0)
my_module/my_script:
js:
js/script.js: {}
# Theme — can explicitly use JS_THEME
my_theme/my_script:
js:
js/script.js: { group: 100 } # JS_THEME
Themes can omit the group and default to JS_DEFAULT, which would make ordering less predictable. But by convention, theme libraries use JS_THEME.
The Full Sort Order
When Drupal's AssetResolver collects and orders JS, it sorts by:
- Group (primary — this is why theme JS wins)
- Weight (secondary — within the same group)
- Attachment order (tertiary — largely incidental)
Module vs Module, Theme vs Theme
Within the same group, modules have no guaranteed ordering relative to each other unless:
- One declares the other as a dependency
- They have different explicit
weightvalues
Practical Implication
If your theme JS needs to override or extend something a module does, the group system has your back — theme JS at JS_THEME will always load after module JS at JS_DEFAULT. But if you have two modules competing, you need explicit dependencies or weight to control their order reliably.
Recent content
-
3 hours 45 minutes ago
-
23 hours 52 minutes ago
-
6 days ago
-
6 days ago
-
1 week ago
-
1 week 1 day ago
-
1 week 1 day ago
-
1 week 2 days ago
-
1 week 5 days ago
-
2 weeks 2 days ago