Languages
[Edit]
EN

Node.js - file exists

0 points
Created by:
Dirask Admin - Marwin
1460

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');

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