3

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!

Edison D'souza
  • 4,551
  • 5
  • 28
  • 39

2 Answers2

2

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";
Edison D'souza
  • 4,551
  • 5
  • 28
  • 39
1

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'
MoreThanTom
  • 526
  • 2
  • 9
  • That is not what I want. 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. – Edison D'souza Oct 24 '18 at 03:50