Search code examples
javascriptwebpackecmascript-6modulenwb

how to export modules in subfolders without including /lib folder in import statement


i'm using nwb to create a new library.

using default settings, nwb will use build project to cjs and es6 and umb and include builds in /lib folder

example project structure

-src
  --ModuleA
    --- index.js
    --- helperForA.js
  --ModuleB
    --- index.js
    --- helperForA.js

  index.js
-lib
  --ModuleA
    --- index.js
    --- helperForA.js
  --ModuleB
    --- index.js
    --- helperForA.js

  --index.js
-package.json

now when i install them into another project i cannot access subfolders without adding /lib prefix first

when i npm install my library into another project currently i have to do

import {A} from 'testLibrary/lib/ModuleA'

but what i want is to do

import {A} from 'testLibrary/ModuleA'

Solution

  • You can use src/index.js to re-export the contents of your library, as per the Libraries docs:

    export A from './ModuleA'
    export B from './ModuleB'