Sometimes, we want to check if a path is absolute or relative with Node.js.
In this article, we’ll look at how to check if a path is absolute or relative with Node.js.
Check if a Path is Absolute or Relative with Node.js
To check if a path is absolute or relative with Node.js, we can use the isAbsolute
method from the path
module.
For instance, we can write:
const path = require('path');
const myPath = '/foo/bar'
if (path.isAbsolute(myPath)) {
console.log('absolute path')
}
to check if myPath
is an absolute path with path.isAbsolute
.
It should return true
since it’s an absolute path.
And therefore, the console log should run.
Conclusion
To check if a path is absolute or relative with Node.js, we can use the isAbsolute
method from the path
module.