ng-packagr: Exporting modules from outside the library folder fails with an error
Type of Issue
[x ] Bug Report
[ ] Feature Request
Description
Iβm trying to export parts of an existing angular app as a library, but it seems there is path issues.
Here is my folder structure:

And my public_api.ts:
export * from '../src/app/shared/shared.module';
But when trying to build the library, I get this error:
Building Angular library from lib/ng-package.json
BUILD ERROR
Error at /Users/noda/github/addon-library/test/lib/.ng_build/ts/public_api.ts:2:15: Cannot find module '../src/app/shared/shared.module'.
Error: Error at /Users/noda/github/addon-library/test/lib/.ng_build/ts/public_api.ts:2:15: Cannot find module '../src/app/shared/shared.module'.
at new UserError (/Users/noda/github/addon-library/test/node_modules/@angular/tsc-wrapped/src/tsc.js:27:28)
at check (/Users/noda/github/addon-library/test/node_modules/@angular/tsc-wrapped/src/tsc.js:93:15)
at Tsc.typeCheck (/Users/noda/github/addon-library/test/node_modules/@angular/tsc-wrapped/src/tsc.js:173:9)
at /Users/noda/github/addon-library/test/node_modules/@angular/tsc-wrapped/src/main.js:122:23
at <anonymous>
How To Reproduce
- Create a typical
angular-cliapp with a feature module. - Create a dedicated
libfolder containingng-packagrconfiguration - Try to export your app feature module in
lib/public_api.tswith something likeexport * from '../src/app/my-feature/my-feature.module'; - Build the library
Expected Behaviour
The library builds without error π
Version Information
ng-packagr: v1.6.0
node: v8.8.1
@angular: v5.0.5
rxjs: 5.5.2
zone.js: 0.8.14
please include any version information that might be relevant, e.g. other third-party libraries
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 24
- Comments: 39 (4 by maintainers)
I get the same error when using sencondary entry points.
I try to build the library according to the following scheme:
Here is a link to the git project of the sample library: https://github.com/annalen/example-library.
This example project is structured as follows:
In this example case, the ViewModule imports the BarModule and the ViewComponent uses the SettingsService.
And I think thatβs why I get the following error.:
Thank you for your help.
I have found a solution that suits my needs.
Context: The
utilspackage used as an example below is meant to be a framework-agnostic tool set that is used across internal applications (vanilla, angular, etc). If the project was written in JS, then any tool in the package could be utilized.While better solution exists in the form of custom NPM scripts, I am also developing some Angular packages alongside, which is why Iβm using Angular CLI to handle the scaffolding and build process. Thatβs why you wonβt see any true Angular modules being used in the example.
Anyways, it made sense to split tools by feature or function into separate packages that would then be able to be imported separately into consuming applications. The
utilspackage evolved into having secondary endpoints with a source structure that resembled:(1) Renamed default βprojectsβ folder generated by Angular CLI. The scope of the project is
@tamu-gisc.(2) Scoped package entry point. The entry point is
@tamu-gisc/utils(3) Secondary entry point for the utils package. The entry point is
@tamu-gisc/utils/geometry(4) Secondary entry point for the utils package. - The entry point is
@tamu-gisc/utils/number(5) Dependency FOR (6)
(7) Specifies the entry file for the secondary entry point:
When
ng build --project=utilswas executed, NgPackagr attempted to build both (3)geometryand (4)numberpackages as secondary entry points for (2)utils. However, when NgPackagr builds the (3)geometrysecondary entry point, it realizes that (5) is not at or in the same package directory relative to (3)geometryβspublic-api.tsentry file specified in (7).The solution I found was reformatting the project structure to the following:
With this layout, all the business logic of the library modules goes into (8)
internal, and (6) has no problem importing (5). The secondary entry point file for the (3)geometrypackage has moved to the root of the (9)utilspackage.The
geometrypackage (7)ng-package.jsonnow looks like:Why this works?
This explanation is potentially wrong, so take it with a grain of salt. However, itβs the best I can do in my own words.
NgPackagr recognizes secondary entry points by the presence of either
package.jsonwith the βngPackageβ property ORng-package.json(Source). Because of this, it is possible to designate secondary entry points with either or bothpackage.jsonorng-package.json(see (3) and (4) in the file tree above) and omitting any source files there. The source files are moved to a βsharedβ space where all packages can access and reference (See (8)). The entry file for each of the packages are alongside the primary entry pointβs entry file (See (9)) and in this way the all source files are at or in the same package directory relative to their respective entry file, meeting the'rootDir' is expected to contain all source files.requirement.I have found this solution from https://github.com/dherges/nx-packaged.
package.json
libs/one-lib/package.json
output
Basically the owne, @dherges, want us to run
ng-packagrfor each library.If so, my question is how we build ONE package to include all library.
Iβm facing the same problem, because I have some internal shared code used for some different libs at the same repo
Iβm also trying to include common code in a number of shared libraries and getting the file outside rootDir issue.
Being able to programmatically compile and override rootDir in a custom tsconfig is not helping, because ng-packagr just overwrites the rootDir I specified in https://github.com/dherges/ng-packagr/blob/master/src/lib/ts/tsconfig.ts#L55
Seems like there is currently no way to include common code in a number of libraries.
I was thinking to have a symbolic link, but itβs kinda uglyβ¦
Iβm building two libraries, on that may be standalone (an icons library) and one (a UI component library) that depends on the icons. I use tsconfig paths to manage references between the two libraries. The only solution that works is to build the standalone library, copy it to my main project node_modules directory (or symlink). This is quirky and makes the build more brittle, however there doesnβt appear to be any other option.
Still having this issue with Angular 6. Thanks to this I have no way to even create a simple common logger service
Edit: solved by using the method suggested above