June 2025
Intermediate to advanced
837 pages
24h 50m
English
But before you start reading or writing files, the first thing you should know is how to find out whether a file exists or not. If you open a file to process it, you don’t have to check for its existence. The open method that creates the file handle returns an error that you can directly act on.
If you want to execute logic based on the existence of a file, you can use the stat method, where you get an object with more information about the corresponding file. Alternatively, you can use the access method to check if you have a certain type of access to the file. Listing 19.4 contains an example for both cases.
import { constants } from 'fs';import { stat, access } from 'fs/promises'; try { const fileStat = await
Read now
Unlock full access