Do you need to use path.join in node.js?

node.jsWindowsUnixPath

node.js Problem Overview


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

node.js Solutions


Solution 1 - node.js

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.

Solution 2 - node.js

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.

Solution 3 - node.js

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

Solution 4 - node.js

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:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaluptonView Question on Stackoverflow
Solution 1 - node.jsebohlmanView Answer on Stackoverflow
Solution 2 - node.jsdronusView Answer on Stackoverflow
Solution 3 - node.jsTimothy StrimpleView Answer on Stackoverflow
Solution 4 - node.jsRafael XavierView Answer on Stackoverflow