night_owl
night_owl

Reputation: 926

No NgModule metadata found for 'AppModule' in Angular 7

Intro

I am trying to create my own custom platform for use in Angular 7. The basic idea is to provide a platform for rendering into a terminal. Here's where I currently am:

The Code

platform-console/console-renderer.ts

(based more or less on Victor Savkin's very excellent tutorial: Experiments with Angular Renderers)

import { Renderer2, RendererStyleFlags2, NgModule, RootRenderer, RendererFactory2, RendererType2, NgZone, APP_INITIALIZER, Injectable } from '@angular/core';

export type ConsoleNode = ConsoleElement | ConsoleText;

export class ConsoleRenderer extends Renderer2 {
  // extremely basic renderer tree management in here -- not important right now
}

@Injectable()
export class ConsoleRendererFactory implements RendererFactory2 {
  public defaultRenderer = new ConsoleRenderer();
  createRenderer(hostElement: ConsoleNode, type: RendererType2): Renderer2 {
    return this.defaultRenderer;
  }
}

export function setUpRenderFlushing(zone: NgZone, rendererFactory: ConsoleRendererFactory) {
  return () => {
    zone.onStable.subscribe(() => {
      console.group('--');
      console.log(rendererFactory.defaultRenderer.root);
      console.log(JSON.stringify(rendererFactory.defaultRenderer.root, null, 2));
      console.groupEnd();
    });
  };
}

platform-console/console.ts

(based loosely on the Angular-supplied platforms)

/* imports, etc */

export const PLATFORM_CONSOLE_ID = 'console';

export const INTERNAL_CONSOLE_PLATFORM_PROVIDERS: StaticProvider[] = [
  { provide: PLATFORM_ID, useValue: PLATFORM_CONSOLE_ID },
  {provide: COMPILER_OPTIONS, useValue: {}, multi: true},
  {provide: CompilerFactory, useClass: JitCompilerFactory, deps: [COMPILER_OPTIONS]},
  { provide: DOCUMENT, useFactory: getDocument, deps: [] }
]

export const platformConsole: (extraProviders?: StaticProvider[]) => PlatformRef =
  createPlatformFactory(platformCore, 'console', INTERNAL_CONSOLE_PLATFORM_PROVIDERS);

export function getDocument(): any {
  return document;
}

export function getErrorHandler(): ErrorHandler {
  return new ErrorHandler();
}

export const CONSOLE_MODULE_PROVIDERS: StaticProvider[] = [
  { provide: APP_ROOT, useValue: true },
  { provide: ErrorHandler, useFactory: getErrorHandler, deps: [] },
  { provide: RendererFactory2, useClass: ConsoleRendererFactory, deps: [] },
  {
    provide: APP_INITIALIZER,
    multi: true,
    useFactory: setUpRenderFlushing,
    deps: [NgZone, RendererFactory2]
  }
];

@NgModule({
  providers: CONSOLE_MODULE_PROVIDERS,
  exports: [CommonModule, ApplicationModule]
})
export class ConsoleModule {
  constructor(@Optional() @SkipSelf() @Inject(ConsoleModule) parentModule: ConsoleModule | null) {
    if (parentModule) {
      throw new Error(
        `ConsoleModule has already been loaded.`
      );
    }
  }
}

main.ts

(in my test app)

const __Zone_disable_XHR = true;
import 'zone.js';
import 'reflect-metadata';

import { enableProdMode, CompilerFactory, COMPILER_OPTIONS } from '@angular/core';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
import { platformConsole } from '../platform_console';

if (environment.production) {
  enableProdMode();
}

platformConsole().bootstrapModule(AppModule)
  .catch(err => console.error(err));

app.module.ts

(in my test app)

/* imports, etc */
@NgModule({
  declarations: [
    AppComponent,
    ChildComponent
  ],
  imports: [
    ConsoleModule
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

I'm using the angular-build:server builder because it seems to be the most straight-forward one of the bunch. Everything else is pretty much what you get after you spin up a brand new Angular app using the CLI.

Error

Everything compiles just fine, into a single main.js file, but when I run it, I get an error:

throw new Error("No NgModule metadata found for '" + stringify(type) + "'.");
                    ^

Error: No NgModule metadata found for 'AppModule'.
    at NgModuleResolver.resolve ((project root)\node_modules\@angular\compiler\bundles\compiler.umd.js:20020:27)
    at CompileMetadataResolver.getNgModuleMetadata ((project root)\node_modules\@angular\compiler\bundles\compiler.umd.js:18662:47)
    at JitCompiler._loadModules ((project root)\node_modules\@angular\compiler\bundles\compiler.umd.js:26085:55)
    at JitCompiler._compileModuleAndComponents ((project root)\node_modules\@angular\compiler\bundles\compiler.umd.js:26066:40)
    at JitCompiler.compileModuleAsync ((project root)\node_modules\@angular\compiler\bundles\compiler.umd.js:26026:41)
    at CompilerImpl.compileModuleAsync ((project root)\node_modules\@angular\platform-browser-dynamic\bundles\platform-browser-dynamic.umd.js:202:35)
    at compileNgModuleFactory__PRE_R3__ ((project root)\node_modules\@angular\core\bundles\core.umd.js:17664:25)
    at PlatformRef.bootstrapModule ((project root)\node_modules\@angular\core\bundles\core.umd.js:17847:20)
    at Module../src/main.ts ((project root)\dist\main.js:444:77)
    at __webpack_require__ ((project root)\dist\main.js:20:30)

What am I missing here? Why isn't my metadata making it into the compiled bundle?

I wonder if it is because I am using a JitCompiler and perhaps the server builder is treating my files as if they were Aot... If that is the case, how do I go about creating and using an AotCompiler without having to write one from scratch?

Notes

I was able to get a successful run using just my renderer shimmed into the platformServer, but I was unable to access Node from inside my components (I imagine there is some browser-y sandboxing at play there). This is a deal-breaker, as I intend to achieve the dream combination of node-level closeness to the OS with all the dynamic/reactive goodness offered by Angular et al. Ultimately, I would like to extend this nascent platform with a proper EventManager driven by Node's events as well as any other services one would expect to have inside a terminal.

I am finding it hard to find information on working in this layer of Angular.

Thank you for your time.

Upvotes: 3

Views: 670

Answers (0)

Related Questions