Syntax Error in Angular App: Unexpected token <

后端 未结 30 1927
独厮守ぢ
独厮守ぢ 2020-12-02 20:16

I have an Angular app which runs perfectly in my local and production environment.. After a tiny change I made, I ran the app locally and it works fine.. Then I built the pr

相关标签:
30条回答
  • 2020-12-02 20:27

    Got this error as well using angular with express.

    I guess when you build an angular project the project is stored in 'dist/the-app' not just 'dist' so this solved my problem on express.

    // Point static path to dist
    app.use(express.static(path.join(__dirname, 'dist/the-app')));
    

    for me, this error was due to not setting the proper path of the built application.

    0 讨论(0)
  • 2020-12-02 20:27

    In my experience i'm add other assets to wwwroot is conflicted to detect static base address for asp.net core.

    For example:

    wwwroot
          |--> assets 
                      |---> ckeditor
    

    Is conflict with angular -> src -> assets and show this error

    Uncaught SyntaxError: Unexpected token < inline.1a152b6….bundle.js:1
    ...
    
    0 讨论(0)
  • 2020-12-02 20:30

    It depends on the server, you are using. For example with Apache, you set the .htaccess file to a relative path on your drive, where your app is located with RewriteBase. Also set the <base href=""> accordingly.

    With node, the

    app.use(express.static(__dirname + '/dist'));
    

    should go before app.get.

    Check also, that the build had no errors and all files were copied to dist.

    0 讨论(0)
  • 2020-12-02 20:31
    <meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    

    add above lines to index.html

    Reason: Chrome caches the index.html so the hash in main.hash.js does not get updated and we get the "unexpected token error <".

    0 讨论(0)
  • 2020-12-02 20:32

    I solved by changing in index.html

    <base href="/">
    

    to

    <base href=".">
    

    or you can set the exact path to the folder that contains your index.html:

    <base href="/path/to/index-folder">
    
    0 讨论(0)
  • 2020-12-02 20:32

    In my case the <!--...--> was the cause of the error

    0 讨论(0)
提交回复
热议问题