Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ESM imports on Windows #3585

Merged
merged 3 commits into from Jan 29, 2023
Merged

Conversation

nchevsky
Copy link
Contributor

When the requireModule() utility function determines that the given fullpath argument corresponds to an ECMAscript module, it tries to import the module as follows:

import(fullpath)

Node.js' ESM loader requires that the given argument be a file:// URL, but fullpath is instead an absolute path whose form varies by operating system:

*nix Windows
fullpath /foo/bar.js c:/foo/bar.js
Outcome ✔️ Succeeds because the path can still be interpreted as a URL. ❌ Fails because the path cannot be interpreted as a URL due to the leading drive letter.

The error message on Windows:

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                                                                                   │
│   There was an error while trying to load the file                                                                │
│   C:\foo\bar.js:                                                                                                  │
│   [ERR_UNSUPPORTED_ESM_URL_SCHEME] Only URLs with a scheme in: file, data are supported by the default ESM        │
│   loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:';                          │
│                                                                                    ^^^^                           │
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

This change solves the problem in a cross-platform fashion by passing fullpath through node:url's pathToFileURL() function, which yields the proper file:// URL that import() expects.


💡 Missing test disclaimer: requireModule() currently has no tests, and I don't see how one can be written without somehow mocking require() to fail with arbitrary error codes and messages. If you feel requireModule() should absolutely have tests and can provide any suggestions, I'm happy to oblige.

When `import()`ing an ES module from the `requireModule()` utility
function, convert the `fullpath` argument to a `file://` URL as expected
by Node.js' ESM loader.

This resolves error `ERR_UNSUPPORTED_ESM_URL_SCHEME` on Windows, where
absolute paths are preceded by a drive letter and cannot be interpreted
as URLs.
@CLAassistant
Copy link

CLAassistant commented Jan 24, 2023

CLA assistant check
All committers have signed the CLA.

@beatfactor beatfactor merged commit 54dcf39 into nightwatchjs:main Jan 29, 2023
harshit-bs pushed a commit to harshit-bs/nightwatch that referenced this pull request Mar 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM imports on Windows fail with ERR_UNSUPPORTED_ESM_URL_SCHEME
3 participants