I am seeing a pattern on some code I have inherited. Each directory has its JS file but there is also a index.js that actually exports items from the other JS file or files.
I presume this is done so you can see exactly what you are exporting, as the main exports are in index.js and the main code is in the other js file or files.
Is this correct? What is this pattern called ?
Should I continue using this pattern.
Let's say I have the following directory structure:
Placing the following code inside the
index.jsfiles......allows you to
requirean entire directory like...The alternative is to
requireeach file individually.Having an
index.jsin a directory is not required. You mayrequirea file in a directory without anindex.jsall the same.However, it gets tedious as your app grows. I use a package like
require-directoryas typing out each file in a directory is also tedious and somewhat error prone.