How to fix the «Invalid template file» / «require_js.phtml» failure for Magento ≥ 2.4.5 in Windows?

Replace

with:

$isWin = 'WIN' === strtoupper(substr(PHP_OS, 0, 3)); /** @var bool $isWin */
foreach ($directories as $directory) {
	if (
		$directory !== null
		&& 0 === strpos($realPath, !$isWin ? $directory : str_replace('/', DIRECTORY_SEPARATOR, $directory))
	) {

For other Magento versions:

  1. For 2.3 ≤ Magento < 2.4.5
  2. For Magento 2.2.x