7

I keep getting this error Uncaught SyntaxError: Unexpected token < whenever I try to run my angular2 application. This is just a modification based on the routing 'tutorial' of the angular2 website.

Normally these kind of errors speak for themselves where I miswrote a piece of code. But the Chrome console tells me the error occurs inside of an angular2 js file.

Reading and trying the answers from both Chrome Uncaught Syntax Error: Unexpected Token ILLEGAL and warning C4819: How to find the character that has to be saved in unicode? didn't work. Guessing that the error has to be somewhere in my boot.ts or app.component.ts.

boot.ts

import {bootstrap}        from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {AppComponent}     from './app.component';
import {HallService}     from './hall/hall.service';
bootstrap(AppComponent,[
    ROUTER_PROVIDERS,
    HallService
]);

app.component.ts

import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {HallCenterComponent} from './hall/hall-center.component';

@Component({
    selector: 'my-app',
    template: `
    <h1 class="title">Component Router</h1>
    <a [routerLink]="['HallCenter']">Hallen</a>
    <a>Heroes</a>
    <router-outlet></router-outlet>
  `,
    directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
    {
        path: '/hall/...',
        name: 'HallCenter',
        component: HallCenterComponent,
        useAsDefault: true
    }
])
export class AppComponent { }

index.html

<html>
<head>
    <base href="/">
    <title>Factory project</title>
    <link rel="stylesheet" href="styles.css">
    <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <script src="node_modules/rxjs/bundles/Rx.js"></script>
    <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
    <script src="node_modules/angular2/bundles/router.dev.js"></script>
    <script>
        System.config({
            packages: {
                app: {
                    format: 'register',
                    defaultExtension: 'js'
                }
            }
        });
        System.import('app/boot')
                .then(null, console.error.bind(console));
    </script>
</head>
<body>
<my-app>loading...</my-app>
</body>
</html>

hall-center.component.ts

import {Component}     from 'angular2/core';
import {RouteConfig, RouterOutlet} from 'angular2/router';
import {HallListComponent}   from './hall-list.component';
import {HallDetailComponent} from './hall-detail.component';
import {HallService}         from './hall.service';

@Component({
    template:  `
    <h2>HALL CENTER</h2>
    <router-outlet></router-outlet>
  `,
    directives: [RouterOutlet],
    providers:  [HallService]
})
@RouteConfig([
    {path:'/',         name: 'HallCenter', component: HallListComponent, useAsDefault: true},
    {path:'/:id',      name: 'HallDetail', component: HallDetailComponent},
    {path:'/list/:id', name: 'HallList',   component: HallListComponent}
])
export class HallCenterComponent { }
Community
  • 1
  • 1
Edito
  • 3,030
  • 13
  • 35
  • 67
  • what do you use as a server? I usually get that kind of errors when I map the routes badly on the server and it returns index.html instead of javascript – toskv Dec 30 '15 at 18:41
  • There are already several questions with similar errors. Did you check them http://stackoverflow.com/search?tab=votes&q=%5bangular2%5d%20unexpected%20token – Günter Zöchbauer Dec 30 '15 at 18:42
  • @Günter Zöchbauer I have tried 5 solutions so far, either I already had them in my code or it didn't help :s – Edito Dec 30 '15 at 19:36

9 Answers9

22

So the above answers seem to be correct but here is some information about the error itself (just for others who run into the same problem wondering what they did wrong) because it can appear with literally each single import and is quite difficult to track down.

It appears whenever a file is required and the webserver is configured to just serve / instead of a 404-file-not-found-page.

/ usually then translates to /index.html and there the first character usually is < (mostly the start of <!DOCTYPE html>) and since that is no valid javascript the browser throws an illegal token exception.

Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
Haringat
  • 221
  • 2
  • 2
5

You have several issues:

you component selector is : 'my-app'

<app>Loading...<app>

should be

<my-app>Loading...</my-app>

Also, you are importing a wrong file:

System.import('app/app');

should be

System.import('app/boot');

Also, unless you are compiling your typescript to java script.. you should change this line and import typescript.js

{defaultExtension: 'js'}}

should be

{defaultExtension: 'ts'}}
<script src="https://code.angularjs.org/tools/typescript.js"></script>

Also, you are missing few imports:

<script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>

Here is a plunker of your code working

Abdulrahman Alsoghayer
  • 16,462
  • 7
  • 51
  • 56
  • Yes sorry, the whole app/my-app was wrong I edited my question, I had 2 projects mixed up, thanks you. – Edito Dec 30 '15 at 20:51
  • Your plunker seems to work fine, however when I make these changes into my project it gives the "Uncaught RangeError: Maximum call stack size exceeded" like said here http://goo.gl/O3EVGP. Even after disabling all my functions, I think I'm going to start from scratch this angular2 stuff is giving me nightmares.. – Edito Dec 30 '15 at 21:23
  • The problem could be on one of these files 'hall-list.component', './hall-detail.component' , './hall.service'.. could you post them? – Abdulrahman Alsoghayer Dec 30 '15 at 21:46
  • I couldn't regenerate your "Maximum call stack" issue. But here is your [plunker](https://plnkr.co/edit/ddBgosBQQN94KbQv40DI?p=preview) working .. I just fixed some imports and removed your 2 comments /* implements OnInit */ << they were causing problems!! – Abdulrahman Alsoghayer Dec 31 '15 at 01:23
3

I suppose, you have installed angular 2 beta release. In this case you have to install additional modules:

npm install es6-promise@^3.0.2 es6-shim@^0.33.3 reflect-metadata@0.1.2 rxjs@^5.0.0-beta.0 zone.js@0.5.10 --save

And import these scripts:

<!-- ES6-related imports -->
<script src="../node_modules/angular2/bundles/angular2-polyfills.min.js"></script>
<script src="../node_modules/es6-shim/es6-shim.min.js"></script>
<script src="../node_modules/systemjs/dist/system.js"></script>
<script>
  //configure system loader
  System.config({defaultJSExtensions: true});
</script>
<script src="../node_modules/rxjs/bundles/Rx.js"></script>
<script src="../node_modules/angular2/bundles/angular2.min.js"></script>
<script src="../node_modules/angular2/bundles/http.min.js"></script>
<script src="../node_modules/angular2/bundles/router.min.js"></script>
<script>
  //bootstrap the Angular2 application
  System.import('app/app').catch(console.log.bind(console));
</script>

EDIT:

In fact, these changes where introduced in alpha 49

Eggy
  • 4,052
  • 7
  • 23
  • 39
  • I just used 'npm install', so normally it has everything installed right? The node_modules folders is about 90mb in size. – Edito Dec 30 '15 at 19:37
  • @Edward If you are using angular 2.0.0-beta.0 those packages have to be installed separately. – Eggy Dec 30 '15 at 19:40
  • ok I will try this, quick question do I only need to import this into my index.html or in every 'class' I'm using angular2 stuff? – Edito Dec 30 '15 at 20:03
  • @Edward Only into `index.html` – Eggy Dec 30 '15 at 20:04
  • Well it fixed the illegal token problem, but instead I get this error "Uncaught RangeError: Maximum call stack size exceeded". I assume this hasn't anything to do with the changes I just made but with my app in general? – Edito Dec 30 '15 at 20:39
  • @Edward Most likely you have somewhere infinite recursive function. – Eggy Dec 30 '15 at 20:44
2

In my case the error came from forgetting to include

<script src="node_modules/angular2/bundles/router.min.js"></script>

in index.html

Michael Gikaru
  • 447
  • 1
  • 6
  • 10
2

In my case I had

import {Observable} from 'rxjs/rx';

which was giving error.

I changed to

import {Observable} from 'rxjs/Rx';

and problem went away. So mind the case-sensitivity.

imahama
  • 51
  • 1
  • I also had some issues sometimes with referencing the directory, for example sometimes `from './rxjs/Rx';` and sometimes it had to be `from 'rxjs/Rx';` can't remember which one where – Edito May 05 '16 at 08:45
0

Ran into this in beta 3 using the rxjs v5.0.0-beta.0 package recommended in the Angular 2 quick start. I switched from the npm package to the one on the Angular beta CDN and it worked fine.

<script src="https://code.angularjs.org/2.0.0-beta.3/Rx.js"></script>
snort
  • 2,285
  • 3
  • 19
  • 21
0

Ran into this today using Angular 2.0.0-beta.0 and rxjs. Had to change

import * as Rx from 'rxjs';

to

import * as Rx from 'rxjs/Rx';
Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
0

make sure to add full path when you import. Also make sure to add ./ in front of files you import which are in the same directory.

So if file.ts wants to import service.ts which resides in the same directory then you shoule write the import like this:

import {service} from './service'

if you write

import {service} from 'service'

then you will get this error

Sul Aga
  • 6,142
  • 5
  • 25
  • 37
0

This is due to SystemJS trying to fetch rxjs methods from a relative path instead of the library itself

I had to insert this line of code inside the Systemjs configuration System.config({})

paths: {
 'rxjs*': 'node_modules/rxjs/bundles/Rx.min.js'
}

Took me several hours to find this solution

source

KhaledMohamedP
  • 5,000
  • 3
  • 28
  • 26