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

Replace the code block:

with the following one:

$isWin = 'WIN' === strtoupper(substr(PHP_OS, 0, 3)); /** @var bool $isWin */
$realPath = $this->fileDriver->getRealPath($path); /** @var string $realPath */
foreach ($directories as $directory) {
	if (0 === strpos($realPath, !$isWin ? $directory : str_replace('/', DIRECTORY_SEPARATOR, $directory))) {

For other Magento versions:

  1. For Magento ≥ 2.4.5
  2. For 2.3 ≤ Magento < 2.4.5

See also: How to fix the «Invalid template file» / «require_js.phtml» failure of Magento ≥ 2.3 in Windows?

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