Home > Software engineering >  NodeJS - Check if random string is a path
NodeJS - Check if random string is a path

Time:12-14

How to check if a string is a directory? I tried casting and using .isDirectory(), not working for me. Also tried something like this:

input = ['123', 'D:'];
let dirs = input.filter(dir => fs.openSync(dir));

but, of course, i get an error with this. I tried wrapping it into try-catch, didn't work. How to check if string like '123' is path?

CodePudding user response:

import fs from "fs";

if (fs.existsSync(string)) {
    return true
}
  • Related