1
votes

I am trying to use Django with Angular2. I have a working Django project. I am getting this error while trying to use Angular2 with it:

Error: Error: No NgModule metadata found for 'AppComponent'.
at new BaseException (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:5116:27)
at NgModuleResolver.resolve (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:12956:27)
at CompileMetadataResolver.getNgModuleMetadata (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:13200:51)
at RuntimeCompiler._compileComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15845:51)
at RuntimeCompiler._compileModuleAndComponents (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15769:41)
at RuntimeCompiler.compileModuleAsync (http://user:8000/ng/node_modules/@angular/compiler//bundles/compiler.umd.js:15746:25)
at PlatformRef_._bootstrapModuleWithZone (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9991:29)
at PlatformRef_.bootstrapModule (http://user:8000/ng/node_modules/@angular/core//bundles/core.umd.js:9984:25)
at Object.eval (http://user:8000/ng/src/main.js:5:53)
at eval (http://user:8000/ng/src/main.js:17:4)
Evaluating http://user:8000/ng/src/main.js
Error loading http://user:8000/ng/src/main.js

Part of package.json file:

"dependencies": {
"@angular/common":  "2.0.0-rc.5",
"@angular/compiler":  "2.0.0-rc.5",
"@angular/core":  "2.0.0-rc.5",
"@angular/forms": "0.2.0",
"@angular/http":  "2.0.0-rc.5",
"@angular/platform-browser":  "2.0.0-rc.5",
"@angular/platform-browser-dynamic":  "2.0.0-rc.5",
"@angular/router":  "3.0.0-beta.2",
"@angular/router-deprecated":  "2.0.0-rc.2",
"@angular/upgrade":  "2.0.0-rc.5",

"systemjs": "0.19.27",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12",

"angular2-in-memory-web-api": "0.0.15",
"bootstrap": "^3.3.6"},

main.ts:

import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppComponent } from './app/app.component';
import {appRouterProviders} from "./app/app.route";
import {HTTP_PROVIDERS} from "@angular/http";
import {enableProdMode} from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module'; <-- Added this as per an answer below. Error is still there. 
platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { RouterModule } from '@angular/router';
import { routes } from './app.routes';

import { AppComponent }  from './app.component';

@NgModule({
  imports:      [ BrowserModule,FormsModule,HttpModule, 
RouterModule.forRoot( ROUTES, { useHash: true } ) ],
declarations: [ AppComponent ],
bootstrap:    [ AppComponent ]
})
export class AppModule { }

index.html:

<script>
  window.ANGULAR_URL = "{{ ANGULAR_URL }}";
  System.import('/ng/src/main.js').catch(function(err){
      console.error(err);
  });
</script>

I have set ANGULAR_URL in settings.py file of Django. It shows /ng/ on browser. I searched a lot and tried changing the dependencies in package.json so many times. I made sure I did npm install every time I made changes in package.json. I have seen many syntax changes with different versions. If this might be the issue, here are the npm and node versions I am using: npm 4.1.1 and node v4.8.3. I am running Django on port 8000.

app.component.ts file:

import { Component } from '@angular/core';
import { ROUTER_DIRECTIVES } from '@angular/router';
@Component({
selector: 'my-app',
template: `
    <h1>My First Angular 2 App</h1>
    <nav>
      <a routerLink="/component1" routerLinkActive="active">Component 1</a>
      <a routerLink="/component2" routerLinkActive="active">Component 2</a>
    </nav>
    <router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
export class AppComponent { }

UPDATE-1

scripts part of package.json file:

"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"docker-build": "docker build -t ng2-quickstart .",
"docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",
"pree2e": "npm run webdriver:update",
"e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
"lint": "tslint ./app/**/*.ts -t verbose",
"lite": "lite-server",
"postinstall": "typings install",
"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"test-once": "tsc && karma start karma.conf.js --single-run",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings",
"webdriver:update": "webdriver-manager update"
},

By the way, I have been following this to use Angular with Django.

UPDATE-2

I have just realised that my tsconfig.json and typings.json are not in same place. I put both in same place as package.json is present. Now I am getting the following error :

error TS5023: Unknown compiler option 'lib'.

UPDATE-3

tsconfig.json file:

{
 "compilerOptions": { 
 "target": "es5",
 "module": "commonjs",
 "moduleResolution": "node",
 "sourceMap": true,
 "emitDecoratorMetadata": true,
 "experimentalDecorators": true,
 "lib": [ "es2015", "dom" ],
 "noImplicitAny": true,
 "suppressImplicitAnyIndexErrors": true
 }
}

And my tsc -v gives Version 2.1.5. Now I have changed the "typescript": "^1.8.10", to "typescript": "^2.1.5". So the error mentioned in update -2 i not there but it shows this:

Error: TypeError: Cannot read property 'forRoot' of undefined

Error: TypeError: Cannot read property 'forRoot' of undefined
at Object.eval (http://user:8000/ng/src/app/app.module.js:22:114)
at eval (http://user:8000/ng/src/app/app.module.js:29:4)
at eval (http://user:8000/ng/src/app/app.module.js:30:3)
at eval (anonymous)
Evaluating http://user:8000/ng/src/app/app.module.js
Evaluating http://user:8000/ng/src/main.js
Error loading http://user:8000/ng/src/main.js

1
can you post also your app.component.ts? - federico scamuzzi
I have included app.component.ts file. - phanny

1 Answers

0
votes

You are not importing AppModule in your main.ts. And you were probably bootstrapping AppComponent which was causing the error you see.

import { bootstrap } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
import {appRouterProviders} from "./app/app.route";
import {HTTP_PROVIDERS} from "@angular/http";
import {enableProdMode} from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

platformBrowserDynamic().bootstrapModule(AppModule);