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

ERR_UNSUPPORTED_ESM_URL_SCHEME when dyanmic import in windows #345

Closed
tangdaoyuan opened this issue Sep 20, 2022 · 9 comments
Closed

ERR_UNSUPPORTED_ESM_URL_SCHEME when dyanmic import in windows #345

tangdaoyuan opened this issue Sep 20, 2022 · 9 comments

Comments

@tangdaoyuan
Copy link

Description

In windows, Using dynamic import with absolute path cause ERR_UNSUPPORTED_ESM_URL_SCHEME error

image

Reproduction

  1. create a.ts file following
import path from "path";

const abs_path = path.resolve('./b.ts', );  // absolute path
import(abs_path)
  1. run node --loader @esbuild-kit/esm-loader a.ts

extra

tsx has same error.

@privatenumber
Copy link
Owner

Apparently this is by design: nodejs/node#34765 (comment)

Does this work in any other Windows environments? eg. different versions of Node.js or TypeScript

@tangdaoyuan
Copy link
Author

Apparently this is by design: nodejs/node#34765 (comment)

Does this work in any other Windows environments? eg. different versions of Node.js or TypeScript

It works after temp fix in win10 with typescript v4.5 ~ v4.8, node v12.20.0, node v14.20.0, node 16.14.2.
I have no other windows enviroments, so I try to create a repo for more tests by github actions.

here is test actions:

@privatenumber privatenumber transferred this issue from esbuild-kit/esm-loader Oct 18, 2023
@privatenumber
Copy link
Owner

Feel free to open a PR

@TimPietrusky
Copy link

TimPietrusky commented Jan 8, 2024

I ran into the same issue, but I got it working when using not an absolute path, but a relative path as configured in the path of tsconfig:

tsconfig

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
}

dynamic import
The file that I want to import exists at src/mydynamicimport.ts.

const name = "mydynamicimport";
await import(`@/${name}`);

Actual problem: absolute path on Windows

So the "issue" here is the usage of an absolute path, which is not working on Windows. You would have to use a proper URL:

import path from "node:path";
import { pathToFileURL } from "node:url";

export async function dynamicImport(fullPath: string) {
  const fileURL = pathToFileURL(fullPath).href;
  return import(fileURL);
}

// Usage example, assuming your mydynamicimport.ts is inside of "src"
await dynamicImport(path.join(process.cwd(), "src", "mydynamicimport"));

This will produce an URL like this: file:///D:/src/mydynamicimport.

@so1ve
Copy link

so1ve commented Feb 4, 2024

It works when dynamic importing a literal:

import('file:///D:/foo.ts') works

@so1ve
Copy link

so1ve commented Feb 4, 2024

Is esbuild-kit/esm-loader#39 a proper fix? I'd like to back-port it to tsx if it works.

@privatenumber
Copy link
Owner

I'm currently wondering if this is even a tsx issue, or if this is a Node.js issue ya'll are trying to patch with tsx?

Specifically, does this work in Node (no tsx) on Windows?

import path from "path"

const abs_path = path.resolve('./b.ts', );  // absolute path
import(abs_path)

To determine this, I'd throw the above snippet in a GitHub repo with CI on ubuntu and windows running it.

@geisterfurz007
Copy link

@privatenumber very much appears to be a Node issue itself

grafik

@privatenumber
Copy link
Owner

Thanks for verifying @geisterfurz007

Closing as it's expected behavior.

@privatenumber privatenumber closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants