question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Function calls are not supported in decorators but 'LoggerModule' was called.

See original GitHub issue

Type of Issue

[x] Bug Report
[ ] Feature Request

Description

I wrote an angular logger library with ng-packagr to use this in different angular applications. The library doesn’t do much more than printing something to the console in the constructor of the service. Everything works fine without --prod, so without AOT compile, in the angular application.

Besides I tried different ng-versions (5.x.x, 6.x.x & 7.x.x). But in all cases everytime (with AOT) the same error when I call LoggerModule.forRoot() in the app.module of the application:

ERROR in Error during template compile of 'AppModule' 
Function calls are not supported in decorators but 'LoggerModule' was called.

I read many articles about this topic, tried different angularCompilerOptions in tsconfig. Any further ideas out there? The module works fine without AOT (but this is no option for us)…

How To Reproduce

Write a library like the usage example of: https://www.npmjs.com/package/ng-packagr

Use this library in a new angular application which is generated by the angular cli by calling LoggerModule.forRoot() in the app.module.ts.

NgModule of the library:

@NgModule({
  declarations: [],
  imports: [],
  providers: []
})
export class LoggerModule {

  static forRoot(): ModuleWithProviders {
    return {
        ngModule: LoggerModule,
        providers: [LoggerService]
    }
  }
}

NgModule of the application:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    LoggerModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent],
  entryComponents: [AppComponent]
})
export class AppModule {
}

Try ng build --prod…

Expected Behaviour

The build of the ng-app works with and without --prod flag / aot compiler.

Version Information

$ node_modules/.bin/ng-packagr --version
ng-packagr: ^4.3.1
@angular/*: ^6.1.0
typescript: ~2.9.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
TimMeissnercommented, Nov 9, 2018

export loggerModule = LoggerModule.forRoot();

add loggerModule to imports, should work

2reactions
christophercrcommented, Feb 1, 2019

I finally found the root cause of the issue: calling throw (or any function) inside the forRoot() breaks the metadata.json generated by ngc.

So, previously I had this:

// inside the NgxFormErrorsModule class

public static forRoot(formErrorsConfig: NgxFormErrorsConfig): ModuleWithProviders {
	if (!formErrorsConfig || !formErrorsConfig.formErrorComponent) {
		throw new Error("NgxFormErrorsModule: a config object should be provided containing the error message component to be used");
	}
	return {
		ngModule: NgxFormErrorsModule,
		providers: [NgxFormErrorsMessageService, { provide: NGX_FORM_ERRORS_CONFIG, useValue: formErrorsConfig }]
	};
}

So, refactoring the method as follows solved the issue:

public static forRoot(formErrorsConfig: NgxFormErrorsConfig): ModuleWithProviders {
	return {
		ngModule: NgxFormErrorsModule,
		providers: [NgxFormErrorsMessageService, { provide: NGX_FORM_ERRORS_CONFIG, useValue: formErrorsConfig }]
	};
}

// now the check is done in the constructor
public constructor(@Optional() @Inject(NGX_FORM_ERRORS_CONFIG) formErrorsConfig: NgxFormErrorsConfig) {
	if (!formErrorsConfig || !formErrorsConfig.formErrorComponent) {
		throw new Error("NgxFormErrorsModule: a config object containing the Error component to be used should be provided via the forRoot() method."
		);
	}
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Function calls are not supported in decorators but ...
trying to setup ngx-formly-material-file, but I got an error: Function calls are not supported in decorators but 'FileTypeModule' was called.
Read more >
Solving AOT Error in NgRx: Function calls are not supported in ...
Function calls are not supported in decorators but 'createAction' was called in 'reducers'. The core explanation for this error is well-covered in the ......
Read more >
Function calls are not supported in decorators ... - Angular Easily
Above error, that is - "Function calls are not supported in decorators but 'ɵmakeDecorator' was called in 'NgModule'" is mostly occurs ...
Read more >
Error in prod build :Function calls are not supported in ...
ERROR in Error during template compile of 'MainModule' Function calls are not supported in decorators but 'InputEditorModule' was called.
Read more >
Function Calls Are Not Supported In Decorators But ... - ADocLib
ACTION: Rewrite the Function Declaration to remove the recursive Function Call.See also: Section 10.3 of the IEEE Std.13642001 IEEE Standard Verilog ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found