If you're searching for a way to import from a module rather than module/dist/Anything
I came across a relatively simple solution.
My folder structure looks something like this:
- project
|- package.json
- app
|- package.json
|- ...
- api
|- package.json
|- ...
- shared
|- package.json
|- tsconfig.json
|- index.ts
|- src
| MySharedResource.ts
| MySharedSomething.ts
...
(shared is an npm package installed locally in both api and app)
I wanted to be able to import things from shared using a simple
import { MySharedResource } from "shared";
The solution was to export every necessary component from MySharedResource.ts
and MySharedSomething.ts
export interface MySharedResource {
...
}
and then export it from index.ts
like this:
export * from "./src/MySharedResource";
export * from "./src/MySharedSomething";
Important note
Your tsconfig.json
in /shared should have the outDir option set to ./dist
(for example), so that index.ts
gets compiled to /shared/dist/index.js
and all other components get compiled into /shared/dist/src.
Also, make sure to set the following in your package.json
:
"main":"dist/index.js"
Note 1
You can export multiple items per file, but if you want imports like this one
import { Thing } from 'shared/Thing';
Then you need another file (e.g., Thing.ts
) alongside index.ts
in /shared with the following content:
export * from "./src/Task";
Note 2
You can also do this
export * as Thing from "./src/Thing";