Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the "root" of a published npm project

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?

like image 633
Rick Avatar asked Oct 17 '22 15:10

Rick


1 Answers

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.

like image 118
Estus Flask Avatar answered Nov 03 '22 23:11

Estus Flask