Check if a file exists with Nodejs
Phong Duong

Phong Duong @phongduong

About: Developer - Learner - Creator

Location:
Vietnam
Joined:
Jul 13, 2018

Check if a file exists with Nodejs

Publish Date: Nov 12 '20
0 0

If you need to check if a file exists without opening or modifying with Nodejs, you use fs.access() method.

const fs = require("fs")

fs.access("file.txt", (err) => { 
    console.log(err ? 'not exist' : 'exist');
  }
)
Enter fullscreen mode Exit fullscreen mode

The callback's argument is an Error object if the check fails.

Comments 0 total

    Add comment