-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
fs.fileReadSync
doesn't understand file://
URL strings
#20357
Comments
I don't remember why it's the case, but I think it is intended that What you can do instead is create an URL object like so: const url = new URL(import.meta.url);
fs.readFileSync(url) |
Yep, this is intentional. The reason is because it would be a non-compatible breaking change and would introduce a non-trivial performance regression even for the common cases. |
Duplicate of #17658. |
Any reason why |
@neroux You need some extra handling for URL percent encoding. Generally, some weirdnesses around encoded See following, which is our implementation of a general purpose URL-to-file path converter Lines 1335 to 1386 in b55a11d
|
@TimothyGu Good points, I guess I focused too much on the provided example :) targos' URL solution does sound like the right approach in that case. |
Description
Assume
/tmp/foo.txt
exist. Reading the file withfs.fileReadSync('/tmp/foo.txt')
works as expected, but usingfs.fileReadSync('file:///tmp/foo.txt')
fails to read the file.Why it matters
I need to read files relative to the current file, but
__dirname
/__filename
are not available when parsing modules. I can compute__dirname
withimport.meta.url
by passing it topath.dirname
, but the path is still prefixed withfile://
.The text was updated successfully, but these errors were encountered: