-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
new URL('.', import.meta.url).pathname produces erroneous path on Windows 10 #37845
Comments
This is the correct output. URLs are not paths. You should convert the url to a path and then perform resolution using that: https://nodejs.org/api/url.html#url_url_fileurltopath_url |
@devsnek Thank you for the clarification, so would you say the following is a canonical equivalent for import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url)) Also, as a follow-up, was there a reason why this equivalent for Thanks :) |
import { fileURLToPath } from 'url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
console.log(__dirname) Yes, I agree this is the correct way to do things. But many developers don't aware of this because they use *nix systems and this creates pain for Windows users. An example: Library https://github.com/bitcoinjs/tiny-secp256k1/blob/master/src_ts/wasm_path.ts Library I think it will be better if Node |
Wasn't unpacking the URL correctly to a file path which resulted in #1 nodejs/node#37845 for context
Code via nodejs/node#37845 (comment), but tested locally to work on Windows now
addressing #182 see nodejs/node#37845
What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
On Linux and macOS, the snippet of code, above, produces the desired outcome, which is to emulate the
__dirname
value that exists in CommonJS in a module in an ECMAScript Modules (ESM) project.If, for example, the above snippet of code is run from
index.js
in/home/aral/sandbox/dirname-test
, the path that is created is the following (and correct):It should, similarly, create a correct path on Windows.
What do you see instead?
The path that is created in Windows is incorrect.
If, for example, the above snippet of code is run from
index.js
inC:\Users\aral\sandbox\dirname-test
, the path that is created is the following (and is wrong):The correct output should be:
Additional information
This updated workaround, based on @devsnek’s reponse, should behave properly across platforms:
(However, the original snippet should also behave correctly on all platforms and doesn’t.)
The text was updated successfully, but these errors were encountered: