While fs.existsSync()
is not deprecated, it's often recommended to use other methods for checking file existence.
err
parameter is null.const fs = require('fs');
// Using fs.access()
fs.access('path/to/file', (err) => {
if (err) {
console.log('File does not exist or is inaccessible');
} else {
console.log('File exists and is accessible');
}
});
// Using fs.stat()
fs.stat('path/to/file', (err, stats) => {
if (err) {
console.log('File does not exist');
} else {
console.log('File exists');
}
});
fs.existsSync()
with symlinks, it may return false even if the symlink exists. This is a known issue and has been reported in the Node.js repository.fs.existsSync()
due to its historical nature and potential issues. However, it's still a part of the Node.js API and can be used if needed.