Search code examples
javascriptnode.jstypescriptcommonjs

how to organize nodejs export in file structure but without create file with the same structure


I have a library that has file structure like this

root
|-- index.js
|-----folder1
        |---- module a
....
.... other modules that under other folders

I don't want consumer of the library to import module a like this

import {...} from "mylibrary/folder1/moudleAIndex"

will be good if developer can just do

import {...} from "mylibrary/moduleA"

is there a way to do above without changing my current file structure?


Solution

  • Create a mylibrary/moduleA.js file with the content:

    import * as moduleA from './folder1/moudleAIndex';
    export moduleA;