Search code examples
javascriptnpmnpmjs

How to expose more than one file in an npm package?


I have an npm package. Let's say example-package. This is normal way of importing.

import RootModule from "example-package";

Now I have one more file nested here.

Package Root > src > Feature > index.js

Now if I have to import this Feature, I would do this.

import Feature from "example-package/src/Feature";

What can I do to avoid developers using my npm package from writing long nested paths and they use something like this.

import Feature from "example-package/Feature";

Just to make it clear, Feature exports multiple options - { A, B ..} . I do not want to import Feature from package and again extract options from Feature. Just want to import it with one slash no matter how long the path is!


Solution

  • I found a solution online. Possible solution would be to create a file /Feature/index.js in the root folder with following content.

    module.exports = require('example-package/src/Feature')
    

    Now you can access it like this,

    import Feature from "example-package/Feature";