How could I read a file in FS without having to put the file extension.
e.x: fs.readFile(../hello**.png(without this file extension?**
Tried this and this returned not found.
How could I read a file in FS without having to put the file extension.
e.x: fs.readFile(../hello**.png(without this file extension?**
Tried this and this returned not found.
2
Answers
As @tkausl said, it is not possible because without the file extension, the operating system doesn’t know the type of file, and can’t read it.
You could try to use the
glob
module to find a file using a certain pattern.Here is an example:
Saw an answer using the glob npm package. And it is possible to use, but not necessary. So, why not stick to the built-in modules of Node.js? No need to install any extra packages…
The code would look like this: