4

I have an angular2 app and at runtime it tries to load a resource from the wrong URL.

It should look for it at: http://localhost:54675/app/services/angular2-jwt.js, but it looks for it at: http://localhost:54675/app/shared/services/angular2-jwt.js. The word shared should not be in the path.

I have this structure in angular2:

app
---services
------angular2-jwt.ts
---shared
------country
---------country.service.ts

In country.service.ts I reference angular2-jwt.ts like this:

import {AuthHttp} from '../../services/angular2-jwt';

As you can see, that's up two (into app) and down into services. It should give this path: app/services/angular2-jwt. I look in the compiled js at looks correct:

System.register(['angular2/core', '../../services/angular2-jwt']

I am using Visual Studio and the intelliSense works. I see AuthHttp in a popup as an option from that library. I don't get an error when I save or build. I think this is correct.

At runtime I get this error in the broswer console:

Error: Unable to load script: http://localhost:54675/app/shared/services/angular2-jwt.js

Notice the script URL path is wrong. It has 'shared' as part of the path but should not.

I have caching disabled in my browser. I look at the js source in the browser and I see that same source mentioned above. It happens in chrome (my default) and Firefox (which I rarely use) so I don't think it's a caching issue. How is it getting this path?

Here is my SystemJS configuraiton from index.html:

<script>
      System.config({
        packages: {
          app: {
            format: 'register',
            defaultExtension: 'js'
          }
        }
      });
      System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

I have now duplicated this on another machine. However, I am importing this same module ('../../services/angular2-jwt') from a different file at a similar locaiton is the directory tree and it works.

Don Chambers
  • 3,798
  • 9
  • 33
  • 74
  • 1
    I made a test with your folder structure and it works on my side. Could you give us your SystemJS configuration? Thanks! – Thierry Templier Feb 10 '16 at 17:59
  • Where do I find the SystemJS configuration? I have the snippet in index.html. I added that to the original post. – Don Chambers Feb 10 '16 at 20:29
  • I have tried ../../../services/angular2-jwt. It does not work. Runtime error, and Intellisense will not wokr with that path. – Don Chambers Feb 10 '16 at 20:30
  • Do you have http://stackoverflow.com/questions/34535163/angular-2-router-no-base-href-set/34535256#34535256 set? Someone mentioned that the `bootstrap(... , [APP_BASE_HREF])` approach doesn't work when the `` tag contains `` tags – Günter Zöchbauer Feb 17 '16 at 14:47
  • I tried it and it didn't help. I don't think this is related to the routing. It happens on the initial page load. – Don Chambers Feb 17 '16 at 15:29
  • Try adding `map: { app: "/" },` to the System.congif (as a sibling/at same level as `package`). – Sasxa Feb 18 '16 at 07:35
  • I tried that and now I get: Uncaught TypeError: Failed to construct 'URL': Invalid URL – Don Chambers Feb 18 '16 at 15:15
  • You are probably importing it wrong from another file. Are you sure this is the file that is causing the issue? – Langley Feb 18 '16 at 17:27
  • I'm sure. If I take it out (and comment out what needs it) then I don't have a problem. Plus, I search for that string with shared and services in the path and nothing is found. – Don Chambers Feb 18 '16 at 17:33
  • Could you provide a small project that replicates this error? – Dan Rasmuson Feb 20 '16 at 21:08
  • @Don Chambers I tried it and it works fine, can you send a small project as "Daniel Rasmuson" sad, Or if you fix it already, tell as why it append? – Nir Schwartz Feb 22 '16 at 08:41

1 Answers1

1

Check the import path to all components, not just this one. I had bad syntax in a parent component and it caused this issue.

  • The file that uses country.service.ts is called country.picker.component. It was referenced from another component like this: import {CountryPickerComponent} from '..//country/country.picker.component'; It had two slashes. I changed this and it work. It's weird that these components worked together but caused the problem in a import down the chain. Notice the two double slashes. Once I changed this to on slash it worked. – Don Chambers Feb 23 '16 at 02:57