url: missing posix/win fileURLtoPath alternative
Currently url.fileURLtoPath produces different output based on the operating system the program is ran on.
I couldn't find an helper similar to path.posix or path.win32.
To solve this issue of: url.fileURLtoPath, you can use this function to create platform-independent file URLs. Here's an example how you can use it:
const url = require('url');
const filePath = 'your/file/path';
const fileUrl = url.pathToFileURL(filePath); // convert file path to file a file URL
const filePathFromUrl = url.fileURLtoPath(fileUrl); // convert file URL back to the file path
console.log('Original file path:', filePath);
console.log('File path from URL:', filePathFromUrl);
Make sure to replace your/file/path with the actual path you want to convert.
Check this might work. 😊
Are there common usecases where url.pathToFileURL() or url.fileURLToPath() are needed, but path module is not?
IMHO these functions belong to path more than to url, and it would make sense to export them on path.posix and path.win32 (and perhaps either re-export or move default version to path), rather than adding optional params or adding url.posix/url.win32.
In my initial idea for #52509, I tried to move them, but I decided against it for a few reasons:
- It's a semver major change (breaking)
- They rely on internal URL features that are exported, so moving them would require a rethink of that system
I'm totally for the potential move, I'm just not sure the best way to do it.