3

I have a number of files in a folder in React. Each of them must be self-initialized. For this, I need to import them. I wouldn’t want to import each file individually. I need to get a list of all the files. But we do not have access to FS on the client. I tried this https://github.com/diegohaz/list-react-files solution, but it does not work (it looks like it somehow uses fs, and gives an error). Can I solve my problem in a straightforward way? Can this be done using the web-pack? Any ideas guys?

Vital O
  • 31
  • 1
  • 5
  • 2
    Possible duplicate of [Get list of filenames in folder with Javascript](https://stackoverflow.com/questions/31274329/get-list-of-filenames-in-folder-with-javascript) – mahan Jul 08 '19 at 07:56

1 Answers1

3

Yes, we don't have fs on the client so we need to each file manually. but there is an easy way of doing it.

Make a registry file where you put the path of file from registry.js relative

{
  "file1": "/folder1/sda.png",
  "file2": "/folder2/asd.png"
}

Now you don't need to import each file manually import registry.json

import registry from registry.json

for x in registry {
  try {
     require(`path to registry/${registry[x]}`)
  } catch (e) {
    console.log(e)
 }
Rahul Rana
  • 455
  • 2
  • 7