Search code examples
javascriptnode.jsecmascript-6ecmascript-5

How to import files that are called the same as the folder that contains them?


When importing files in ECMAScript, we do import Home from './Home', and it's going to import import Home from './Home/index'.

I'm in a project where there is no index file. Instead, we have a file called the same as the folder that contains it. For example:

src/component/Home/Home.js
src/component/Footer/Footer.js

Is there a way to configure NODE_PATH or something, to continue doing import Home from './Home' and import import Home from './Home/Home'?


Solution

  • As Madara Uchiha said, this is not possible. You could do something clever like this:

    In src/component/index.js:

    export { default as Home } from "./Home/Home";
    export { default as Footer } from "./Footer/Footer";
    

    Elsewhere:

    import { Home, Footer } from "./component";
    

    Hope this helps.