I'm publishing an npm package named foo
to the npm registry.
I wrote the package using a compile-to-js language.
For sanity, I put the compiled output into the dist/
folder of the project directory.
My package.json
lists the entrypoint as dist/entry.js
:
{
"name": "foo",
"main": "dist/entry.js",
}
Sometimes, I want to use files within the package that are not part of the entry point. For example, there is a very useful export called whatever
inside of dist/util.js
:
import { whatever } from "foo/dist/util";
This works, but forcing the users of my package to type dist/
in all import
statements is inconvenient.
Furthermore, re-exporting every possible util function is not DRY. I do not want to re-export from the entrypoint.
Ideally, I would like to import files from dist/
using the following syntax:
import { whatever } from "foo/util"
How do I configure my package.json
to search for files in the dist/
folder of my project?
This cannot be done.
This is the reason why some packages have entry point file that re-exports all public exports (not everything that resides in dist
is intended to be used by end user), e.g. @angular/core
.
And the reason why some packages have unsuitable file structure that is published to NPM registry and favours proper import paths, e.g. rxjs
.
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