Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a file exists in typescript

Tags:

typescript

I am new to typescript and need to check if a certain system is installed on the machine, which is windows. I'm thinking of doing this by checking for the existence of some files, but I'm not finding how to do this check in typescript. Would anyone have any idea how to do it?

like image 815
Patricia Avatar asked Nov 30 '17 13:11

Patricia


2 Answers

I found this approach much simpler:

import fs from 'fs';
if (fs.existsSync(path)) {
  // File exists in path
} else {
  // File doesn't exist in path
}
like image 152
yahavi Avatar answered Oct 16 '22 12:10

yahavi


fs.stat(path, (exists) => {
            if (exists == null) {
                return true;
            } else if (exists.code === 'ENOENT') {
                return false;
            }
        });
like image 25
Carlos Onofre Avatar answered Oct 16 '22 12:10

Carlos Onofre