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

840 Views Asked by At

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!

2

There are 2 best solutions below

0
Edison D'souza On BEST ANSWER

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";
1
MoreThanTom On

You can add the feature as an export of your index -

index.js:

import Feature from './Feature.js'
export Feature

Then anyone using the package can just import like

import { Feature } from 'example-package'