Service is not under 'rootDir' in Angular secondary entry point
Asked Answered
S

2

6

When compiling an Angular library, I am getting this error concerning rootDir of my sub libraries

library/services/src/public-api.ts:31:15 - error TS6059: File 'C:/libname/library/services/src/orders/orders.service.ts' is not under 'rootDir' 'C:/libname\library\classes\src'. 'rootDir' is expected to contain all source files.

I have come to understanding that it is not a good practice to use relative imports inside Angular library between individual secondary entries. So I have divided my code into secondary entries + setup paths in TSConfig

Code structure

library
  services
    src
      public-api.ts
    package.json
  models
    src
      public-api.ts
    package.json
  src
    public-api.ts
  package.json

TSConfig

{
  "rootDir": "./library/",
  "paths": {
    "@core/services": [ "library/services/src/public-api.ts" ]
  }
}

angular.json

{
  "projects": {
    "lib": {
      "root": "library",
      "sourceRoot": "library"
    }
  }
}

So the question is - how to fix imports between individual secondary entry points or file structure and make the project compile okay? I understand that the compilation of secondary entry points is separate and "treated as a separate project", hence the error. Should I therefore add individual secondary entry points as peer dependencies?

Of course there are more paths and secondary endpoints - But that is irrelevant for the issue, this is enough to show my file structure and setup

Shaggy answered 2/1, 2021 at 12:12 Comment(1)
did you ever figure this out? running into the same issuesFarkas
I
2

you may use relative imports which results in a file outside of the root directory of the secondary entrypoint, eg. import Users from "../../models/src/public_api.ts"

Instead, import the module using the name of the library, eg. import Users from "library/models"

This issue from ng-packagr repo provides good code examples.

Innovation answered 13/3, 2021 at 13:20 Comment(0)
T
1

also can happen when IDE import helper generates nonrelative path (e.g. "src/sevices/.." instead of "../../services")

actually had the issue in primary entrypoint part of the lib when a component imported service using this kind of autogenerated path (yet throwing error during compilation of secondary entrypoint, go figure...)

Taboo answered 30/5, 2022 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.