Absolute Windows paths are handled as invalid URL path in ESM import()

  • Version: v14.8v0
  • Platform: Windows
  • Subsystem:

Related: #31710

What steps will reproduce the bug?

Windows paths are interpreted as invalid file URL. In Linux and Mac, the issue can't be reproduced.

Fail

await import("D:\repositories\fastify-autoload\routes\hello.mjs")

Works

await import(url.pathToFileURL("D:\repositories\fastify-autoload\routes\hello.mjs").href)

Root cause

#31710 (comment)

What is the expected behavior?

Interpret windows paths as valid URL paths so we can provide consistent cross-platform behaviour.

What do you see instead?

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:698:11)
    at Loader.resolve (internal/modules/esm/loader.js:82:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:226:28)
    at Loader.import (internal/modules/esm/loader.js:161:28)
    at importModuleDynamically (internal/modules/cjs/loader.js:1144:27)
    at exports.importModuleDynamicallyCallback (internal/process/esm_loader.js:27:14)
    at loadPlugin (D:\repositories\fastify-autoload\index.js:118:5)
    at D:\repositories\fastify-autoload\index.js:28:12
    at Array.map (<anonymous>)
    at fastifyAutoload (D:\repositories\fastify-autoload\index.js:27:29) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

@mcollina