Search code examples
angularsystemjs

Error after deploy Angular 2 app


I'm trying to deploy sample Angular 2 app into my hosting. I have done everything like in: https://angular.io/guide/deployment and everyrhing is ok when I'm running this app on localhost.

But when I put all files to hosting I'm getting errors in browser console:

Error: (SystemJS) Unexpected token <
SyntaxError: Unexpected token <
    at eval (<anonymous>)
    at ZoneDelegate.invoke (https://unpkg.com/[email protected]?main=browser:365:26)
    at Zone.run (https://unpkg.com/[email protected]?main=browser:125:43)
    at https://unpkg.com/[email protected]?main=browser:760:57
    at ZoneDelegate.invokeTask (https://unpkg.com/[email protected]?main=browser:398:31)

Here is my index.html file:

<!DOCTYPE html>
<html>
  <head>
    <!-- Doesn't load from node_modules! -->

    <!-- Set the base href -->
    <base href="/">
    <title>Simple Deployment</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

    <!-- Polyfills -->
    <script src="https://unpkg.com/core-js/client/shim.min.js"></script>

    <!-- Update these package versions as needed -->
    <script src="https://unpkg.com/[email protected]?main=browser"></script>
    <script src="https://unpkg.com/[email protected]/dist/system.src.js">    </script>

    <!-- This SystemJS configuration loads umd packages from the web -->
    <script src="systemjs.config.server.js"></script>

    <script>
      System.import('main.js')
            .catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>loading...</my-app>
  </body>

</html>

And systemjs.config file:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      'npm:': 'https://unpkg.com/' // path serves as alias
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      'app': 'app',

      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.min.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.min.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.min.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.min.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.min.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.min.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.min.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.min.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.min.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.min.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.min.js',

      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        defaultExtension: 'js',
        meta: {
          './*.js': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

I tried to change those files, but still with no effect. When I delete this lines from index.html file I don't get any errors, but of course page is not loading:

<script src="systemjs.config.server.js"></script>

<script>
  System.import('main.js')
        .catch(function(err){ console.error(err); });
</script>

Solution

  • Ok, I found a solution for this problem.

    I moved my index.html file into root folder and changed base href:

    <base href="/src/app">