Issue Bug Closed

#108874JavaScriptRenderer throws unclear TypeError when module cannot be resolved

Himanshu Ramavat 02/05/26, 15:06 updated 03/31/26, 08:10

Description

*Problem*
When `ImportMap::resolveImport()` returns `null` for a JavaScript module (e.g. module not in import map or misconfigured), `JavaScriptRenderer::render()` fails with a generic PHP TypeError:

TypeError: TYPO3\CMS\Core\Page\JavaScriptRenderer::{closure}(): Argument #1 ($url) must be of type string, null given
in vendor/typo3/cms-core/Classes/Page/JavaScriptRenderer.php line 137
The closure used in `array_map()` is typed as `fn(string $url)` but receives `null` from the `$modules` array. The error does not indicate which module failed to resolve, making debugging difficult.

*Root cause*
- `$modules` is built from `$this->importMap->resolveImport($instruction->getName())` which returns `?string`.
- When a module cannot be resolved, `resolveImport()` returns `null`.
- The code then passes `$modules` (values only) to `array_map()`, so the failing module name is never available in the error.

**Expected behavior**
When a module cannot be resolved, the code should throw a clear exception that includes the module name (e.g. `@my/extension/SomeModule`), so developers can fix the missing or wrong import configuration.

**Proposed solution**
- Replace the `array_map()` over `$modules` with a `foreach ($modules as $moduleName => $url)`.
- Before creating each script tag, validate that `$url` is a non-empty string.
- If not, throw a `\RuntimeException` with a message like:
`JavaScript module “%s” could not be resolved (import map returned no src URL).`

Reviews 3

History 27

  1. Himanshu Ramavat commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-02-06T15:00:10+00:00" > 02/06/26, 15:00 </span>

    Applied in changeset commit:886a2ba5d0197f9ec2085037d437d62740e4d789.