How to check if a path is absolute or relative

Manuel Di Iorio picture Manuel Di Iorio · Feb 11, 2014 · Viewed 17.1k times · Source

UNIX absolute path starts with '/', whereas Windows starts with alphabet 'C:' or '\'. Does node.js has a standard multiplatform function to check if a path is absolute or relative ?

Answer

chresse picture chresse · May 26, 2015

Since node version 0.12.0 you can use the path.isAbsolute(path) function from the path module.

i.e:

var path = require('path');
if(path.isAbsolute(myPath)) {
    //...
}