Sitelet https://github.com/nodejs/node/issues/41521
Skip to content

Add a utility to convert file URL or path to one of them? #41521

Description

@fisker

What is the problem this feature will solve?

It's a nice way to accept both URL and path string in API, but we need covert it to either string or URL, it would be nice to have a built-in method to do this.

What is the feature you are proposing to solve the problem?

For converting to string:

Maybe add path.from(URL | string)? @sindresorhus proposed here.

For converting to URL:

url.from(URL | string)

I'm not sure, since URL is a standard, I guess adding method URL object won't be acceptable, maybe to node:url module?

What alternatives have you considered?

No response

Activity

  1. changed the title [-]Add a utility to convert file URL and path to path?[/-] [+]Add a utility to convert between file URL or path to one of them?[/+] on Jan 14, 2022
  2. changed the title [-]Add a utility to convert between file URL or path to one of them?[/-] [+]Add a utility to convert file URL or path to one of them?[/+] on Jan 14, 2022
  3. aduh95 commented on Jan 14, 2022

    @aduh95
    Contributor

    We have a toPathIfFileURL internal API already, maybe we could expose it in the node:url module

    node/lib/internal/url.js

    Lines 1558 to 1562 in 38b7961

    function toPathIfFileURL(fileURLOrPath) {
    if (!isURLInstance(fileURLOrPath))
    return fileURLOrPath;
    return fileURLToPath(fileURLOrPath);
    }

    For converting to URL:

    url.from(URL | string)

    I don't think there is any reliable way to check if a string is a path or a file URL... I'm afraid the best option here is to assume that a string is a path and call pathToFileurl(/sitelet?url=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2Fstring) (which is what is doing Node.js internally as well) – or if you prefer to assume strings are URLs in your application, call new url(/sitelet?url=https%3A%2F%2Fgithub.com%2Fnodejs%2Fnode%2Fissues%2Fstring).

  4. added
    urlIssues and PRs related to the legacy built-in url module.
    on Jan 14, 2022
  5. sindresorhus commented on Jan 15, 2022

    @sindresorhus

    We have a toPathIfFileURL internal API already, maybe we could expose it in the node:url module

    Wouldn't it be more natural to have it in node:path, since we want to convert it to a path? My proposal for path.from() was inspired by Array.from and Buffer.from type methods.

  6. sindresorhus commented on Jan 15, 2022

    @sindresorhus

    I don't think there is any reliable way to check if a string is a path or a file URL...

    To be clear, the string in that example is meant to be a file path, not a string file URL.

    Example usage:

    import path from 'node:path';
    
    // Passthrough
    path.from('/Users/sindresorhus/dev');
    //=> '/Users/sindresorhus/dev'
    
    path.from(new URL('file:///Users/sindresorhus/dev'));
    //=> '/Users/sindresorhus/dev'
  7. moved this to Pending triage in Node.js feature requestson Jan 16, 2022
  8. benjamingr commented on Feb 16, 2022

    @benjamingr
    Member
  9. fisker commented on Feb 17, 2022

    @fisker
    ContributorAuthor

    Yes, that's what I want.

  10. benjamingr commented on Feb 17, 2022

    @benjamingr
    Member

    @aduh95 wdyt about an implementation that does that? ( #41521 (comment) )

  11. aduh95 commented on Feb 17, 2022

    @aduh95
    Contributor

    @aduh95 wdyt about an implementation that does that? ( #41521 (comment) )

    I don't think we'll get consensus landing in core something that treats differently a string depending on if it starts with file: or not – what if the user meant ./file:/ (i.e. they really have a local directory named file:)?

    The workarounds that come to mind are:

    • only support absolute paths – or require relative paths to start with a . (like in require calls).
    • always treat a string as a path.
    • publish this as an npm module instead (I think npm module would get away with not supporting having directory named file:).

    Any combination of the above would address my concern, but I'm open to other ideas if someone thinks of something else.

  12. benjamingr commented on Feb 17, 2022

    @benjamingr
    Member

    @aduh95 I think it's safe to say "if this can be an npm module, sindre has already probably published one" :D Making small useful modules kind of their thing.

    Edit: lol found it https://www.npmjs.com/package/file-url
    Edit2: actually that's the other side of conversion

  13. benjamingr commented on Feb 17, 2022

    @benjamingr
    Member

    Basically: I think the main issue with this being on npm is that file urls and paths are both very common in modern Node.js and not having a utility to convert them in core is weird.

    I'm sure we can bikeshed the name and semantics as long as the core use case (convert file url to path) is addressed.

    Would it be better if the name was explicit path.fromFileUrl that only takes either a file:// url string or an actual URL with the file protocol and throws an error on non-file urls?

  14. 29 remaining items

  15. moved this from Awaiting Triage to Triaged in Node.js feature requestson Jun 26, 2024
  16. github-actions commented on Dec 23, 2024

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  17. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Dec 23, 2024
  18. sindresorhus commented on Dec 23, 2024

    @sindresorhus

    Please keep this open.

  19. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Dec 23, 2024
  20. zthrigger commented on Feb 23, 2025

    @zthrigger

    Hmm

  21. github-actions commented on Aug 23, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  22. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 23, 2025
  23. added
    never-staleIssues and PRs exempt from automated stale handling.
    and removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Aug 23, 2025
  24. sindresorhus commented on Aug 23, 2025

    @sindresorhus

    Please keep this open.

  25. avivkeller commented on Jul 28, 2026

    @avivkeller
    Member

    I think there's no current need for another API for file path conversions, the URL module should provide everything y'all need, right? (Just ping me if I'm wrong)

  26. sindresorhus commented on Jul 29, 2026

    @sindresorhus

    fileURLToPath() handles only the URL branch; it doesn't accept a URL | string input and pass paths through unchanged.

    The actual pattern every caller has to duplicate is:

    const path = input instanceof URL ? fileURLToPath(input) : input;

    That is precisely the API requested by this issue.

    A small path.from(URL | string) API is unambiguous, useful at every API boundary, and removes the same boilerplate from userland.

    @avivkeller

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

    feature requestIssues requesting new Node.js features.never-staleIssues and PRs exempt from automated stale handling.urlIssues and PRs related to the legacy built-in url module.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions