Scoped packages throw compilation errors
jdjuan opened this issue ยท 10 comments
When I try to compile an application for production that uses a library which has an NPM scope, it throws errors and fails.
Reproduction Steps
- Create a new library:
mkdir lib
,cd lib
, andyo angular2-library
- Generate a new clean angular app with angular-cli:
ng new app-seed
- Modify your library
package.json
(the one insrc
, or thedist
folder) to be scoped:"name": "@aaa/lib",
- Perform an
NPM link
of your library:npm link
and then inside your app:npm link @aaa/lib
- Run
ng build --prod --aot
and see the console error below. Commandsng serve
andng build
work well.
You can also clone this repo and jump to step 3.
Error
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/app/app.component.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/node_modules/@aaa/lib/lib.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/app/app.module.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/app/app.module.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/app/app.component.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve 'lib' in 'C:\Users\jherrera\repos\PROD-ISSUE3\app-seed\src\$$_gendir\app'
@ ./src/$$_gendir/app/app.module.ngfactory.ts 23:0-31
@ ./src/main.ts
@ multi ./src/main.ts
ERROR in ./src/$$_gendir/app/app.component.ngfactory.ts
Module not found: Error: Can't resolve 'lib' in 'C:\Users\jherrera\repos\PROD-ISSUE3\app-seed\src\$$_gendir\app'
@ ./src/$$_gendir/app/app.component.ngfactory.ts 10:0-31
@ ./src/$$_gendir/app/app.module.ngfactory.ts
@ ./src/main.ts
@ multi ./src/main.ts
ERROR in C:/Users/jherrera/repos/PROD-ISSUE3/app-seed/src/$$_gendir/node_modules/@aaa/lib/lib.ngfactory.ts (1,1): Cannot find module 'lib'.
ERROR in ./src/$$_gendir/~/@aaa/lib/lib.ngfactory.ts
Module not found: Error: Can't resolve 'lib' in 'C:\Users\jherrera\repos\PROD-ISSUE3\app-seed\src\$$_gendir\node_modules\@aaa\lib'
@ ./src/$$_gendir/~/@aaa/lib/lib.ngfactory.ts 18:0-31
@ ./src/$$_gendir/app/app.component.ngfactory.ts
@ ./src/$$_gendir/app/app.module.ngfactory.ts
@ ./src/main.ts
@ multi ./src/main.ts
Environment
- NPM: 4.5.0
- Node: 7.9.0
- Angular CLI: 1.0.0
- Generator-angular2-libary: 9.0.0
- OS: Windows 10
Nevermind I just fixed. It turns out that whenever you are creating scoped packages you want to update the package name in the package.json
like this:
"name": "@aaa/lib"
AND, you also need to update flatModuleId
in your tsconfig.es5.json
, like this:
"flatModuleId": "@aaa/lib"
Once you do that, you have scoped packages working correctly.
PS: We can close this now, but don't you think it is worth creating a wiki page for people who want to create scoped packages with this generator? I really spend a few days trying to figure it out @jvandemo
@jdjuan โ Thank you for your follow-up, Juan. Much appreciated! ๐
I have added a section to the README with instructions and a link to this issue.
Thanks so much for this @jdjuan ! I'd tried the same fix before but I'd made the mistake of changing the "flatModuleOutFile" to include the scope too, which stopped it from building. Anyone else that made the same mistake, ONLY change "name" and "flatModuleId", you don't need to change "flatModuleOutFile".
@DaveMonag โ Thank you for your follow-up ๐
We still had this issue when trying to build using AOT (angular cli --prod
mode) an app that's using a library generated by this even after following the guideline to update the flatModuleId
.
The fix in this case was in gulp.js
file - by removing the .umd
suffix of the generated JS file name in the umd distribution pipe.
So for @aaa/lib
the generated file should be aaa-lib.js
instead of aaa-lib.umd.js
for angular to build properly.
@odolha โ Thank you, what error were you seeing when you used the .umd.js
suffix? Thanks!
Sure, it was like this:
ERROR in ./src/$$_gendir/app/app.module.ngfactory.ts
Module not found: Error: Can't resolve '@aaa/lib/aaa-lib' in '\src$$_gendir\app'
@ ./src/$$_gendir/app/app.module.ngfactory.ts 21:0-58
@ ./src/main.ts
@ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts
And a bunch of other errors like this for each import of something from the module that we have.
From what I can gather, when using the --prod
flag, angular/webpack tries to bundle the lib code from the prepared file node_modules/@aaa/lib/aaa-lib.js
which initially was named node_modules/@aaa/lib/aaa-lib.umd.js
by this library.
This occurred for Angular 2.4.10
that we still use both for our app and library. We also use an older angular-cli 1.0.0-rc.4
, due to some other compatibility complications.
@odolha โ Did you have a main
property in your package.json that referenced the UMD bundle?
I think adding main: 'aaa-lib.umd.js'
to package.json would also have solved the issue.
Would you be able to verify that? Thanks in advance!