JavaScript ordering in asset libraries in Drupal

Posted by admin
Wednesday March 11th , 2026 4:38 p.m.



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

JS_LIBRARY

-100

Core/contrib low-level libs

JS_DEFAULT

0

Modules

JS_THEME

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:

  1. Group (primary — this is why theme JS wins)
  2. Weight (secondary — within the same group)
  3. 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 weight values

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.