
Warning: Don't use fs.exists(path, callback) as it is deprecated.
Reason: The fs.exists method uses a callback that is inconsistent with other Node.js callbacks.
What should we use then?
The fs.existsSync(path) method can be used as it does not use any callback.
It takes one argument i.e. path as a string and returns true if the file exists and vice versa.
An example is shown below:
var fs = require('fs');
// fs.existsSync(path) returns boolean
if (fs.existsSync(path)) {
console.log('File exists!');
} else{
console.log('Sorry, File does not exists!');
}
Usually, it’s not recommended to use fs.existsSync as it can break your asynchronous code due to unnecessary blocking.
So, what’s the next option?
Well, according to the latest Node.js v11.7.0 Documentation, fs.stat(path[, options], callback) and fs.access(path[, mode], callback) are suggested as alternatives.
Lets, look more deep into it!
1) fs.access(path[, mode], callback)
It takes three arguments as follows:
- path – string for a file path
- mode- an optional argument, specifies the accessibility checks to be performed
- callback – invokes if an error occurs
const file = 'package.json';
// Check if the file exists in the current directory.
fs.access(file, fs.constants.F_OK, (err) => {
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
});
// Check if the file is readable.
fs.access(file, fs.constants.R_OK, (err) => {
console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
});
// Check if the file is writable.
fs.access(file, fs.constants.W_OK, (err) => {
console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
});
// Check if the file exists in the current directory, and if it is writable.
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
if (err) {
console.error(
`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
} else {
console.log(`${file} exists, and it is writable`);
}
});
According to NodeJS documentation, if a user wants to check file existence without manipulating it afterward, fs.access()
is recommended. As it checks the existence of a file without opening it.
2) fs.stat(path[, options], callback)
It takes three arguments as follows:
- path – string for a file path
- options – an optional argument, an object which holds the value of bigint.
- callback – returns error or Stats object depending if an error occurred or not.
var fs = require('fs');
fs.stat('path', function(err) {
if (!err) {
console.log('File exists!');
}
else if (err.code === 'ENOENT') {
console.log('Sorry, file does not exist!');
}else {
console.log(err.code);
}
});
Returns an object Stats with lots of elements as given below:
Stats {
dev: 3803513792,
mode: 33206,
nlink: 1,
uid: 0,
gid: 0,
rdev: 0,
blksize: undefined,
ino: 281474976744241,
size: 227700,
blocks: undefined,
atimeMs: 1518123325605.2195,
mtimeMs: 1456549122349.375,
ctimeMs: 1456549122349.375,
birthtimeMs: 1518123325605.2195,
atime: 2018-02-08T20:55:25.605Z,
mtime: 2016-02-27T04:58:42.349Z,
ctime: 2016-02-27T04:58:42.349Z,
birthtime: 2018-02-08T20:55:25.605Z }
How to check if a file exists before reading or writing on a file?
If a user wants to check for the existence of a file before reading or writing, then a user should do it directly and handle the error manually as shown below:
Check before writing on a file
s.open('myfile', 'wx', (err, fd) => {
if (err) {
if (err.code === 'EEXIST') {
console.error('myfile already exists');
return;
}
throw err;
}
writeMyData(fd);
});
Check before reading a file
fs.open('myfile', 'r', (err, fd) => {
if (err) {
if (err.code === 'ENOENT') {
console.error('myfile does not exist');
return;
}
throw err;
}
readMyData(fd);
});
Refer to official NodeJS documentation for more information.