Search code examples
typescriptdeno

How can one check if a file or directory exists using Deno?


The Deno TypeScript runtime has built-in functions, but none of them address checking the existence of a file or directory. How can one check if a file or directory exists?


Solution

  • There is the standard library implementation, here: https://deno.land/std/fs/mod.ts

    import {existsSync} from "https://deno.land/std/fs/mod.ts";
    
    const pathFound = existsSync(filePath)
    console.log(pathFound)
    

    This code will print true if the path exists and false if not.

    And this is the async implementation:

    import {exists} from "https://deno.land/std/fs/mod.ts"
    
    exists(filePath).then((result : boolean) => console.log(result))
    

    Make sure you run deno with the unstable flag and grant access to that file:

    deno run --unstable  --allow-read={filePath} index.ts