Skip to content

url: missing posix/win fileURLtoPath alternative #51609

Description

@mcollina

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.

Activity

  1. Shreyas-29 commented on Feb 9, 2024

    @Shreyas-29

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

  2. LiviaMedeiros commented on Apr 14, 2024

    @LiviaMedeiros
    Member

    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.

  3. avivkeller commented on Apr 14, 2024

    @avivkeller
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions