Reputation: 1617
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
Upvotes: 6
Views: 1860
Reputation: 1354
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...)
Upvotes: 1
Reputation: 645
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.
Upvotes: 2