Skip to content
Advertisement

How to check if my input file is valid and existed in Javascript?

I am getting strings from a text file in Javascript. I just want to show a message whenever the user enters the wrong file name or invalid file name. like this: console.log(“You input is invalid”);
My code to read my text file and transfer the data to my variable is as follow:

JavaScript

I don’t know how to check the existence of the file and put it in an if-else statement.

Advertisement

Answer

You can use existsSync

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement