Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Feature Request: Make path.normalize() expand "~/" #2857

Closed

Description

@cvee

It would be extremely useful to have path.normalize expand HOME directory references containing the tilde.

Activity

  1. jonlong commented on Mar 2, 2012

    @jonlong

    YES, +1 for this!

  2. isaacs commented on Mar 2, 2012

    @isaacs

    This can't be done in path.normalize. path is only a string manipulation library.

    It would be good to have a cross-platform /etc/passwd type library to look up user info, though. (Just reading /etc/passwd is not actually sufficient, since even systems with this file don't necessarily put all the relevant data into it, and Windows obviously doesn't use that same file.)

  3. cvee commented on Mar 2, 2012

    @cvee
    Author

    I was under the impression path.normalize worked similarly to [NSString stringByStandardizingPath] in Foundation.

    If this isn't the case I'd love to see a new path function that provides similar functionality.

  4. stevesims commented on Apr 19, 2012

    @stevesims

    Oh so very this.

    I fully expected path.normalize to understand , and was shocked to find that it didn't. Seeing a supposedly resolved path that includes a "" in the middle of it is bizarre at best.

    IMHO as a bare minimum if node.js's path module is to not understand ~ then it should throw an error if it encounters it.

  5. bnoordhuis commented on Apr 19, 2012

    @bnoordhuis
    Member

    IMHO as a bare minimum if node.js's path module is to not understand ~ then it should throw an error if it encounters it.

    ~ is a valid path character.

  6. rogeriopvl commented on May 11, 2012

    @rogeriopvl

    +1 for this.

    This could be done in normalize or create a new function expandPath or something similar. This feature is useful for node CLI apps with config files editable by users, IMHO.

  7. isaacs commented on May 11, 2012

    @isaacs

    If we're going to provide access to home directories, we're going to do it properly with a cross-platform passwd binding. IMO, that should go into libuv, since it relies on performing IO and doing platform-specific fiddly bits.

    @piscisaureus: your thoughts?

    We are not going to add this to path.resolve or path.normalize in a way that is only half-way, and sends the message that users can expect it to work.

    If you want to just replace it with process.env.HOME, you can do so quite easily. However, full-on support for ~ path semantics (along with ~user/foo/bar etc.) belongs in a library like node-glob, not in the path module, which is strictly for string munging, and cannot perform IO.

  8. breck7 commented on Mar 3, 2013

    @breck7

    It seems @isaacs understands the side effects of doing this the short way and wants to do it the right way which makes sense.

    For those like me that just need a quick fix:

    function resolvePath (string) {
    if (string.substr(0,1) === '~')
    string = process.env.HOME + string.substr(1)
    return path.resolve(string)
    }

  9. rex commented on Jun 9, 2013

    @rex

    @breck7 Thank you so very much for providing that snippet, I already have it implemented and it works like a charm, exactly as one would expect. I am lazy enough of a programmer to love not having to think of simple fixes, but not so lazy as to forgo expressing appreciation to the author of said fixes :)

    @isaacs I came into this thread fully prepared to be frustrated about path() not handling tilde ~ in path names. After reading your reasoning, I am reminded once again that I am so very much in my infancy as a programmer and that I still have a lot to learn where it concerns the bigger picture.

    After putting some thought into it, expanding the ~ in the path would do a few things that I can see:

    Good: Allow programmers to not have to write the resolvePath() method that @breck7 so generously posted.

    BAD: Give better programmers (myself very much not included) the false confidence that they can perform far more complex operations on the home directory and other disparate parts of the operating environment, thus potentially causing severe issues and other badness.

    Every time I get mad at npm or Node, it always seems to end up being my immaturity as a developer causing me to make simple mistakes (sometimes not so simple). Which is good for me, because it's made me a shitload better.

    This is one of those times.

    Edit: Reposting @breck7's resolvePath() method within a code block for formatting purposes.

    // @breck7   https://github.com/breck7
    function resolvePath (string) {
      if (string.substr(0,1) === '~')
        string = process.env.HOME + string.substr(1)
      return path.resolve(string)
    }
  10. sindresorhus commented on Jun 24, 2013

    @sindresorhus

    👍 I too was expecting this to work. Would be a useful addition.

    In the meantime I did a module for this: untildify

  11. blockloop commented on Aug 9, 2013

    @blockloop

    +1 for this

  12. jacobsvante commented on Feb 24, 2014

    @jacobsvante

    How can this not be in the stdlib?

  13. kroggen commented on May 19, 2014

    @kroggen

    +1 for this

    here is an enhanced version of @breck7 function that works on Windows too:

    function resolvePath (string) {
      if (string.substr(0,1) === '~') {
        homedir = (process.platform.substr(0, 3) === 'win') ? process.env.HOMEPATH : process.env.HOME;
        string = homedir + string.substr(1)
      }
      return path.resolve(string)
    }
  14. yanickrochon commented on May 21, 2014

    @yanickrochon

    @kroggen I would remove OS checks altogether and simply check for env variables. Since process.env.HOME is probably the most used key, I'd have your snippet slightly changed into

    function resolvePath(str) {
      if (str.substr(0, 2) === '~/') {
        str = (process.env.HOME || process.env.HOMEPATH || process.env.HOMEDIR || process.cwd()) + str.substr(1);
      }
      return path.resolve(str);
    }

    Also, your snippet might convert paths like "~foo/bar" into "/home/foofoo/bar".

  15. jcblw commented on Sep 7, 2014

    @jcblw

    👍 I was confused when this didnt work with normalize.

    For now @sindresorhus's module works great. Thanks

  16. reggi commented on Nov 26, 2014

    @reggi

    +1 Would love to see this in path!
    Thanks for the module @sindresorhus! 🌈 ☔ 🐬

  17. jasnell commented on May 18, 2015

    @jasnell
    Member

    @joyent/node-coreteam ... io.js decided not to pursue this. There are userland modules supporting this function. Closing.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions