Inconsistency in path.join() on Windows #3610

ethanl opened this Issue Jul 2, 2012 · 10 comments

7 participants


On windows:

var path = require('path');
path.join('/', '/foo'); // returns \\foo
path.join('/', '/foo/bar'); // returns \\\\foo\\bar\\  <-- this looks incorrect

On *nix:

var path = require('path');
path.join('/', '/foo'); // returns /foo
path.join('/', '/foo/bar'); // returns /foo/bar

@piscisaureus both windows joins look strange to me "\" usually denotes a UNC path with a server path
if '/' was expected to be the 'root' directory, on Windows this might need to include the current drive? so the join would result in 'c:\foo' or something similar?


Should not have to include the drive letter, otherwise portable applications on USB dongles with their arbitrary drive letters could somewhat suffer.

On Windows '\\foo' means the \foo string (slash within the '-quoted string constant is doubled according to JavaScript rules); you may try console.log(path.join('/', '/foo')) and get \foo. That's just a path from the root.

However, console.log(path.join('/', '/foo/bar')) returns (writes) \\foo\bar\ — that's a UNC path and looks like a bug.

Node.js Foundation member

This is why I always suggest that people use path.resolve instead of path.join :-)

I think this particular "problem" can be fixed but there is a more general problem with path.join, which is that there are situations where the join operation is ambiguous. For example:

path.join('c:', 'windows'); // Should that be c:\windows or c:windows ?

c:\windows, I guess.

I understand that windows path could be relative to the current directory (instead of the root directory) on c:, and thus c:windows, but I cannot imagine someone would actually want that.

Node.js Foundation member

We can't change this in 0.8. @piscisaureus has a plan for master.


Valid paths are:
C:asd -> relative with prefix
asd -> relative without prefix
C:/asd -> absolute with prefix
/asd -> absolute without prefix
//asd -> UNC

So here we have a problem with /asd and //asd. Should we consider //asd an invalid path? No. And this?

> require ("path").normalize ("//asd//asd")
> require ("path").normalize ("//asd/asd")

The first begin with 2 valid slashes but normalize is bugged and returns only one slash. The second is correct because I submitted months ago an error with UNC: #3066, but new errors were introduced.

Finally, windows paths seems to be fixed corretcly. For backward compatibility I ended with this:
meta-path. The code fixes all path related bugs.

Node.js Foundation member

Unfortunately it's too dangerous to fix this in v0.8. A fix for master (future 0.10) was landed in bc93883.


I got same error on node v6

AngeIII commented May 12, 2016 edited

join('../something') => '..\something'

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