user1908375
user1908375

Reputation: 1089

angular2: SyntaxError: Unexpected token <(…)

I know, this question was asked already, but I can't find the solution for my particular case I can't not understand real reason of the error.

I have an angularjs2 app which is running fine. Now I would like to import marked library.

What I did:

npm install marked
tsd install marked --save

and the tsd.json

{
  "version": "v4",
  "repo": "borisyankov/DefinitelyTyped",
  "ref": "master",
  "path": "typings",
  "bundle": "typings/tsd.d.ts",
  "installed": {
    "marked/marked.d.ts": {
      "commit": "cc3d223a946f661eff871787edeb0fcb8f0db156"
    }
  }
}

now trying to import "marked" into my component

import {Component} from 'angular2/core';
import * as marked from 'marked';

@Component({
  selector: 'blog-component',
  templateUrl: 'app/components/blog/blog.html'
})
export class BlogComponent {
  private md: MarkedStatic;

  constructor() {
    this.md = marked.setOptions({});
  }

  getMarked() {
    return this.md.parse("# HELLO");
  }
}

This line: this.md = marked.setOptions({}); produces the error with SyntaxError: Unexpected token.. removing this line does not end with an error.. I also thing that MarkedStatic was imported correclty then. but then ist not possible to parse markdown, because it should be first initialized whith setOptions.

So I assume that importing of marked fails, or the setOptions method fails.. but I can't figure why...

and here the script part of my index.html:

<!-- 1. Load libraries -->
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/typescript/lib/typescript.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 src="node_modules/angular2/bundles/http.dev.js"></script>
<script src="node_modules/marked/marked.min.js"></script>
<script>
      System.config({
        transpiler: 'typescript',
        typescriptOptions: { emitDecoratorMetadata: true },
        packages: {'app': {defaultExtension: 'ts'}}
      });
      System
        .import('app/boot')
        .then(null, console.error.bind(console));
    </script>

Upvotes: 3

Views: 836

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202176

You need to add this in your SystemJS configuration instead of including it into a script element:

<script>
  System.config({
    transpiler: 'typescript',
    typescriptOptions: { emitDecoratorMetadata: true },
    map: {
      marked: 'node_modules/marked/marked.min.js'
    },
    packages: {'app': {defaultExtension: 'ts'}}
  });
</script>

See this plunkr: https://plnkr.co/edit/0oSeaIyMWoq5fAKKlJLA?p=preview.

This question could be useful for you:

Upvotes: 2

Related Questions