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

Do you need to use path.join in 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.


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.


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