Checking if a file already exists in the given path with Node js
- 2023年3月28日
- 技術情報
Today, I would like to share about checking if a file already exists in the given path using Node js. Let’s take a look.
To check if a file already exists or not, we can perform simply by using existsSync() method of ‘fs’ module in node js. The code is as follow.
const fs = require('fs')
let filePath = "C:\\Users\\waith\\Documents\\t1ee.pdf"
if (!fs.existsSync(filePath)) {
console.log('File does NOT exist!');
}else{
console.log('File exists.');
}
Yes, that is it. The steps to perfom that is very simple. It is just to declare fs module and use fs.existsSync() method to check the filepath.
This is all for now. Hope you enjoy that.
By Asahi
waithaw at 2023年03月28日 10:00:00