Languages
[Edit]
EN

Node.js - file exists

0 points
Created by:
Diana
720

In this article, we would like to show you how to check that file is exists using Node.js.

Practical examples

Synchronous version:

const fs = require('fs');

const path = './path/to/file.txt';

try {
    if (fs.existsSync(path)) {
        console.log('File exists!');
    } else {
        console.log('File does not exist!');
    }
} catch (error) {
    console.error(error);
}

Asynchronous version:

const fs = require('fs/promises');

const fileExists = async (path) => {
    try {
        await fs.access(path);
        console.log('File exists!');
    } catch (error) {
        console.error(error.message);
    }
};

fileExists('./path/to/file.txt');
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - file system module

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join