Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing non "main" modules? #68

Closed
momsse opened this issue May 15, 2021 · 2 comments
Closed

Importing non "main" modules? #68

momsse opened this issue May 15, 2021 · 2 comments

Comments

@momsse
Copy link

momsse commented May 15, 2021

Hi @NiGhTTraX,

Thanks for the article and sharing your work 馃憣

I tried to add an extra foo/src/demo.ts module and importing it from bar/src/index.ts after adding files: ["dist"] on foo/package.json.

import demo from "@nighttrax/foo/src/demo";
import demo from "@nighttrax/foo/demo";

Both syntaxes ends with Error: Cannot find module '@nighttrax/foo/[src/]demo

Is it possible to import non "main" modules (without having to import and export them from the main one)?

All typescript mono repo examples that I found so far showcase the same kind of example of package a import module b's single exported main module.

@NiGhTTraX
Copy link
Owner

Hi @momsse, this is answered in #53. The TL;DR is:

  • you can add a new path alias before the "main" one, like so:
"paths": {
  // This need to be first, so index imports can be matched by the next alias.
  // You can add or omit the `src` part here, depending on your preference.
  //                                 V
  "@nighttrax/foo/*": ["packages/foo/*"],

  "@nighttrax/*": ["packages/*/src"]
}
  • doing the above will break publishing, unless you:
    • use the aliases during building as well,
    • publish from within the dist/ folder, after you've copied your package.json there.
  • using imports like this makes library APIs more brittle, as changes in the folder structure can break consumers.

@momsse
Copy link
Author

momsse commented May 16, 2021

Thanks you for the help, will try this soon 馃檹

@momsse momsse closed this as completed May 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants