5

Maybe i am making it to hard (and i should just look for a prefix of ./ and ../) but I don't want to re-invent the wheel and write a function to correctly detect relative paths (for all platforms, etc.)

Existing library?

Are there npm packages that do this? Surely this problem has been solved...

Approaches?

Barring an existing library, my intended approach was to use the path module functions to join the possibly relative path to a known prefix, and then see what the result was, with the assumption that path.join('some_base', possiblyRelative) would allow some sort of distinguishing characteristic in a platform safe way.

Any other suggestions? Approaches?

Community
  • 1
  • 1
el2iot2
  • 6,188
  • 6
  • 35
  • 49

3 Answers3

10

UPDATE2: TomDotTom found a more robust answer here: How to check if a path is absolute or relative

Reproduced here (but in the inverse):

var path = require('path');
function isRelative(p) {
  return path.normalize(p + '/') !== path.normalize(path.resolve(p) + '/');
}

For node v0.12 and above, I recommend path.isAbsolute instead.

Community
  • 1
  • 1
B T
  • 46,771
  • 31
  • 164
  • 191
  • isRelative('/foo/bar/) >> false – TomDotTom Jun 08 '15 at 16:21
  • @TomDotTom '/foo/bar' is not a relative path in linux. Care to explain more why you think that's an incorrect result? – B T Jun 15 '15 at 19:13
  • @B_T the isRelative function above will return false for '/foo/bar' indicating it's not a relative path. – TomDotTom Jun 26 '15 at 09:00
  • @TomDotTom '/foo/bar' *isn't* a relative path.. 'foo/bar' is. – B T Jun 26 '15 at 17:44
  • @B_T I suggest you open up node and try out the code: isRelative('/foo/bar') >> false; isRelative('foo/bar') >> true – TomDotTom Jul 14 '15 at 08:43
  • YES TOM, THAT IS THE CORRECT BEHAVIOR – B T Jul 14 '15 at 18:36
  • 1
    @B_T apologies I wasn't paying attention and seem to have accidentally been trolling you :-/. But I still think there is an issue, if you could run these four examples and comment on the one marked (in my opinion) inccorect: [OK] isRelative('foo/bar') >> true; [OK isRelative('foo/bar/') >> true; [OK] isRelative('/foo/bar') >> false; [INCORRECT] isRelative('/foo/bar/') >> true – TomDotTom Jul 15 '15 at 09:23
  • Ah, ok you do have a point there. Looks like `resolve` adds a trailing '/' and `normalize` doesn't. Thanks for persisting in telling me what's wrong! I think I can fix this by removing the trailing slash from the output of `resolve` – B T Jul 15 '15 at 19:36
  • @TomDotTom Ugh, fixing this issue is harder than it looks – B T Jul 15 '15 at 19:45
  • 2
    @B_T shameless plug http://stackoverflow.com/questions/21698906/how-to-check-if-a-path-is-absolute-or-relative/30714706#30714706 – TomDotTom Jul 17 '15 at 12:12
  • Hope you don't mind me using your answer to fix mine – B T Jul 18 '15 at 02:08
8

It's a little late but for others searching on the same issue:

since node version 0.12.0 you have the the path.isAbsolute(path) function from the path module. To detect if your path is relative use the negation of it:

i.e:

var path = require('path');
if( ! path.isAbsolute(myPath)) {
    //...
}
Sake
  • 3,813
  • 6
  • 29
  • 35
chresse
  • 4,049
  • 2
  • 24
  • 37
0
function isRelative(str)
{
    //Remove quotes as it can potentially mess up the string
    str=str.replace("\'\"",'');
    return str.substring(0,2)=="./"||str.substring(0,3)=="../"; 
}

In this example we only allow relative strings for the beginning of the string path