[wp-trac] [WordPress Trac] #65165: Script module dependencies may be unavailable on evaluation
WordPress Trac
noreply at wordpress.org
Tue May 5 09:43:55 UTC 2026
#65165: Script module dependencies may be unavailable on evaluation
---------------------------+--------------------
Reporter: jonsurrell | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: 7.0
Component: Script Loader | Version: trunk
Severity: normal | Keywords:
Focuses: javascript |
---------------------------+--------------------
[61587] / #61500 added `module_dependencies` to classic scripts, allowing
for scripts to depend on registered modules.
Scripts with module dependencies may throw:
> TypeError: Failed to resolve module specifier 'example-module'
For example:
{{{#!php
<?php
wp_register_script_module(
'example-module',
'/path/to/example-module.mjs',
array(),
VERSION
);
wp_register_script(
'test-script-module-dependency',
false,
array(),
false,
array(
'module_dependencies' => array( 'example-module' ),
// ‼️ Enabling one or both of these fixes the issue
// 'in_footer' => true,
// 'strategy' => 'defer',
),
);
wp_enqueue_script( 'test-script-module-dependency' );
wp_add_inline_script(
'test-script-module-dependency',
<<<'JAVASCRIPT'
(async () => {
const m = await import('example-module');
console.log(m);
})();
JAVASCRIPT
);
}}}
This eagerly evaluated script will attempt to resolve the `'example-
module'` module specifier before the importmap has been printed and will
throw the above error. The HTML will be printed like this:
{{{#!xml
<head>
<!-- … -->
<script id="test-script-module-dependency-js-after">/*…*/</script>
</head>
<body>
<!-- … -->
<script type="importmap">{"imports":{"…":"…"}}</script>
</body>
}}}
Note that the exact ordering depends on several conditions, such as
whether the request is in the WP Admin area, or on the frontend whether or
not a block theme is used. However, in my testing the importmap is
consistently being printed after the dependent script.
Note the comment in the example. It seems that if the dependent script is
deferred or printed in the footer (or ideally, both) then the problem
seems to be resolved because the importmap will have been printed already.
Scripts dependencies on modules are a new feature planned for 7.0 added in
[61587].
--
Ticket URL: <https://core.trac.wordpress.org/ticket/65165>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list