148

as everyone knows Windows does paths with backslashes where Unix does paths with forward slashes. node.js provides path.join() to always use the correct slash. So for example instead of writing the Unix only 'a/b/c' you would do path.join('a','b','c') instead.

However, it seems that despite this difference if you do not normalize your paths (e.g. using path.join) and just write paths like a/b/c node.js has no problem with running your scripts on Windows.

So is there any benefit over writing path.join('a','b','c') over 'a/b/c'? Both appear to work regardless of platform...

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
balupton
  • 47,113
  • 32
  • 131
  • 182

4 Answers4

112

Windows filesystems have no problem using either forward or backward slashes as path separators (this has been the case since back in the DOS days). The only real issue is that Windows command-line processors (or, more specifically, Windows-native command-line utilities) tend to interpret forward slashes as option specifiers rather than path components. Therefore, you need a backslashed path if you need to pass a path to a Windows command run as a subprocess. Also, Windows API calls (and methods from higher-level languages that call the Windows API) that return paths will use backslashes, so even if you aren't passing them to subprocesses, you'll need to normalize them.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
105

path.join will take care of unneccessary delimiters, that may occur if the given pathes come from unknown sources (eg. user input, 3rd party APIs etc.).

So path.join('a/','b') path.join('a/','/b'), path.join('a','b') and path.join('a','/b') will all give a/b.

Without using it, you usually would make expectations about the start and end of the pathes joined, knowing they only have no or one slash.

dronus
  • 10,774
  • 8
  • 54
  • 80
  • 1
    That sounds a bit useful but receiving arbitrary unchecked paths from unknown sources sounds like a big security problem. That's not something to do often. – Gherman May 08 '20 at 15:55
51

I use path.join to ensure folder separators are in the correct places, not necessarily to ensure that it uses forward versus back slashes. For example:

path.join("/var/www", "test")

Will correctly insert the separator between www and test /var/www/test

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Timothy Strimple
  • 22,920
  • 6
  • 69
  • 76
  • 3
    I don't understand this point. If you have those scripts in variables, why not just add a slash manually? – mgol Dec 20 '13 at 20:34
  • 3
    I also have troubles understanding this answer. Cannot see any value. – oligofren Feb 24 '14 at 13:39
  • 26
    Because I'm not always sure whether the path values I get from other sources will have trailing slashes or not. My example above was contrived. Often those paths aren't hard coded, but are being pulled from other config files, user input, libraries, etc. – Timothy Strimple Feb 25 '14 at 04:49
  • @TimothyStrimple ~ a good place to use `path.join` would be in your other answer here http://stackoverflow.com/questions/9027648/proper-way-to-organize-myapp-routes#answer-9030181. That answer led me here to another question answered by yourself :) – Pebbl Mar 16 '14 at 17:20
  • 25
    I was also skeptical about that answer until 5 min later my code blew up on `return baseDir + relativePath + filename;`. I replaced it right away by `return path.join(baseDir, relativePath, filename);`. It's indeed very helpful! – Pedro Aug 11 '14 at 00:39
  • I see the purpose of this method. example: path.join("/var/www/", "/test") will also work. Note the double //. path.join will fix it. Good when you're calling url and dont know if its ending in / or not or starting with / or not. path.join takes care of this mess. – Nhon Ha Oct 08 '19 at 03:15
  • Looks like people in this comments section are not aware that `path///to///a` is the same as `path/to/a`. – André Chalella May 13 '22 at 18:08
  • @AndréChalella That is an operating system feature, not a feature of node.js. For example, C:\path\to\a is absolutely not the same at C:\path\\\to\\\a in Windows. Path.join helps protects you from these differences. – Timothy Strimple May 18 '22 at 22:48
35

Short answer:

All fs.* functions (eg. fs.open, etc) treats the pathname for you. So, you don't need to use path.join yourself and make your code illegible.

Long answer:

All fs.* functions call path._makeLong(path), which in turn call path.resolve(path), which has special RegExps for Windows, which takes into account backslash \ or forward slashes /. You can check it out for yourself looking their source code at:

Rafael Xavier
  • 2,840
  • 27
  • 34