1

I am serving my index.html from nodejs server. This ng2 file works fine with the live-server. but when I load it from nodejs index.js with specific routes it gives the following error.

Uncaught SyntaxError: Unexpected token <
VM105:17Uncaught ReferenceError: System is not defined

I have loaded the files right. Here are the loaded files:

<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.16/system-polyfills.js"></script>
<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.min.js"></script>
<script src="node_modules/angular2/bundles/angular2.min.js"></script>
<script src="node_modules/angular2/bundles/router.js"></script>
<script src="node_modules/angular2/bundles/http.js"></script>

<!-- 2. Configure SystemJS and Import -->
<script>
    System.config({
    packages: {        
      client: {
        format: 'register',
        defaultExtension: 'js'
      }
    }
  });
  System.import('client/boot')
        .then(null, console.error.bind(console));
</script>

What am I missing? All the files are served from the node server.

Gary
  • 2,293
  • 2
  • 25
  • 47

1 Answers1

2

Most of time when you have the error Uncaught SyntaxError: Unexpected token <, this means that there a 404 error on the file referenced within a script element.

I think that you Node serve doesn't make available your node_modules folder. So the node_modules/systemjs/dist/system.src.js file can't be loaded (it's also true for other files in node_modules).

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • I am not getting 404 errors for node_modules. Loading the whole folder as static at this moment. – Gary Feb 24 '16 at 08:31
  • All files can be loaded? You can see successful requests in the Network tab of developer tools? – Thierry Templier Feb 24 '16 at 08:32
  • 404 errors don't typically cause browsers to try to parse the JS at all. Usually it is because the src points to an HTML document. (It might be an incorrect URL that *should* be a 404 but is being handled wrong by the server though). – Quentin Feb 24 '16 at 08:34
  • Yes, you're right in the case of 404, an error payload in the HTML format can be received. So JavaScript can't evaluate this... – Thierry Templier Feb 24 '16 at 08:37
  • `System` is provided by the `node_modules/systemjs/dist/system.src.js` file. So if you have the message `System is not defined`, I guess that there is problem when loading this file... Could you check the content of this file in your browser in the Source tab of developer tools? – Thierry Templier Feb 24 '16 at 08:40
  • 1
    Ok thanks. the cdn files worked. Let me check the node index.js pathings and serving. I was looking at this link below. Thank you very much. http://stackoverflow.com/questions/34323219/angular-2-systemjs-import – Gary Feb 24 '16 at 08:44
  • 1
    You're welcome! Yes I think the problem is at this level ;-) – Thierry Templier Feb 24 '16 at 08:49
  • oh god I debugged this thing for hours and I forgot about my nodejs permissions :) – Tiberiu Popescu May 06 '16 at 23:07