I recently published a private module to NPM which has some common code we use across a few of our services.
The code is written in ES6 and so we need to transpile it using babel before publishing to NPM. I've got a prepublish script which transpiles src
in to lib
.
There's no index.js
file in this module since it's just some common code.
The problem I'm having is that when I install the module from NPM, using require('@ourorg/ourmodule/somecode')
doesn't work (module can't be found). I instead have to use require('@ourorg/ourmodule/lib/somecode')
.
I've tried changing the main
field in package.json
to many variations of lib
, but it doesn't seem to work unless I include an index.js
file, in which case require('@ourorg/ourmodule')
returns whatever is exported there. One workaround I can see would be to export all the common code in an index.js
file, but this isn't maintainable at all.
The main
field in package.json
follows the same rules as normal Node imports - either it should point at a single file in particular, or it can point at a directory that has an index.js
in it.
As far as I know, there is no way to make importing your package simply be an alias for a directory. If there was, what would require("@ourorg/ourmodule")
return?
If it's absolutely driving you crazy having to type lib
every time you import something, maybe you could add a step to your build process that autogenerates an index.js
that re-exports everything at the root?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With