how to use the exists function from the fs-extra library in typescript

To use the exists function from the fs-extra library in typescript, you can follow these steps:

  1. Install fs-extra as a dependency in your Typescript project:
index.ts
npm install --save fs-extra
28 chars
2 lines
  1. Import the exists function from fs-extra at the top of your TypeScript file:
index.ts
import { exists } from "fs-extra";
35 chars
2 lines
  1. You can now use the exists function to check if a file or directory exists:
index.ts
const path = "/path/to/file/or/directory";

exists(path).then((exists:boolean) => {
	if(exists){
		console.log(`The file/directory at ${path} exists`);
	} else {
		console.log(`The file/directory at ${path} does not exist`);
	}
}).catch((e:Error) => {
	console.log(`Error checking if the file/directory exists - ${e.message}`);
});
332 chars
12 lines

The exists function returns a Promise that resolves to true if the path exists, and false if the path does not exist.

Note: The exists function from fs-extra is a promise-based function, so you need to use .then() and .catch() to handle the result and any errors respectively.

gistlibby LogSnag