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

[Node.js] Dynamic import of routes in Windows #24

Open
rtritto opened this issue Oct 2, 2024 · 0 comments · May be fixed by #30
Open

[Node.js] Dynamic import of routes in Windows #24

rtritto opened this issue Oct 2, 2024 · 0 comments · May be fixed by #30

Comments

@rtritto
Copy link
Contributor

rtritto commented Oct 2, 2024

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

Related:

Fix

const file = await import(fullPath);

+ import { pathToFileURL } from 'node:url';
// ...
const fullPath = path.join(directoryPath, filePath);
- const file = await import(fullPath);
+ const file = await import(pathToFileURL(fullPath).href);  // import parameter is file:///C:/<FILEPATH>

Alternative fixes

  1. (hacky but performant)
    https://stackoverflow.com/questions/69665780/error-err-unsupported-esm-url-scheme-only-file-and-data-urls-are-supported-by/70057245#70057245
    const fullPath = path.join(directoryPath, filePath);
    - const file = await import(fullPath);
    + const file = await import(`file://${fullPath}`);  // import parameter is file://C:\<FILEPATH>
    OR
    + import path from 'node:path';
    // ...
    const fullPath = path.join(directoryPath, filePath);
    - const file = await import(fullPath);
    + const file = await import(`file://${path.resolve(directoryPath, file)}`);  // import parameter is file://C:\<FILEPATH>
    Similar usage with @brillout/import
    https://github.com/brillout/import/blob/ba848455442484eb258aaa2d9864d4848e4ed0fb/index.ts#L4-L34
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 a pull request may close this issue.

1 participant