node icon indicating copy to clipboard operation
node copied to clipboard

url: missing posix/win fileURLtoPath alternative

Open mcollina opened this issue 1 year ago • 1 comments

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.

mcollina avatar Jan 30 '24 18:01 mcollina

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. 😊

Shreyas-29 avatar Feb 09 '24 10:02 Shreyas-29

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.

LiviaMedeiros avatar Apr 14 '24 20:04 LiviaMedeiros

In my initial idea for #52509, I tried to move them, but I decided against it for a few reasons:

  1. It's a semver major change (breaking)
  2. 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.

avivkeller avatar Apr 14 '24 20:04 avivkeller