3
votes

I am new to Angular 2 and to get started, I download Quickstart project from the official website.

But it shows me the following error in console:

GET http://localhost:3000/node_modules/@angular/platf...-dynamic/bundles/platform-browser-dynamic.umd.js

304 Not Modified 87ms zone.js (line 2019) GET http://localhost:3000/systemjs-angular-loader.js 404 Not Found 2ms zone.js (line 2019)

"NetworkError: 404 Not Found - http://localhost:3000/systemjs-angular-loader.js" systemj...ader.js

Unhandled Promise rejection: Permission denied to access property "then" ; Zone: ; Task: Promise.then ; Value: Error: Permission denied to access property "then"

resolvePromise@http://localhost:3000/node_modules/zone.js/dist/zone.js:622:21

scheduleResolveOrReject/<@http://localhost:3000/node_modules/zone.js/dist/zone.js:716:17

ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:367:17

Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:166:28

drainMicroTaskQueue@http://localhost:3000/node_modules/zone.js/dist/zone.js:546:25

ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:424:25

5

5 Answers

3
votes

change your systemjs.config.js with this systemjs.config.js

(function (global) {
  System.config({
    map: {
      'rxjs': 'node_modules/rxjs',
      '@angular': 'node_modules/@angular',
     'app': './app',
     'angular2-in-memory-web-api' : 'node_modules/angular2-in-memory-web-api'
   },
    packages: {
      'app': {
        main: 'main.js',
        defaultExtension: 'js'
      },

      '@angular/platform-browser': {
        main: 'bundles/platform-browser.umd.js'
      },

      '@angular/core': {
        main: 'bundles/core.umd.js'
      },

      '@angular/http': {
        main: 'bundles/http.umd.js'
      },

      '@angular/compiler': {
        main: 'bundles/compiler.umd.js'
      },

      '@angular/compiler-cli': {
        main: 'index.js'
      },

      '@angular/router': {
        main: 'bundles/router.umd.js'
      },

      '@angular/upgrade': {
        main: 'bundles/upgrade.umd.js'
      },

      '@angular/forms': {
        main: 'bundles/forms.umd.js'
      },

      '@angular/common': {
        main: 'bundles/common.umd.js',
        defaultExtension: 'js'
      },

      '@angular/platform-browser-dynamic': {
        main: 'bundles/platform-browser-dynamic.umd.js'
      },

      '@angular/platform-server': {
        main: 'bundles/platform-server.umd.js'
      },

      'rxjs': {
        defaultExtension: 'js'
      },

      'angular2-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);
2
votes

It seems that Angular.io team included systemjs-angular-loader.js into the plunker links, however forgot to include the file into downloadable projects.

Please add systemjs-angular-loader.js to the same level as systemjs.config.js

var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

module.exports.translate = function(load){

  var url = new URL(load.address);

  var basePathParts = url.pathname.split('/');

  if (url.href.indexOf('plnkr') != -1) {
    basePathParts.shift();
    basePathParts.shift();
  }

  basePathParts.pop();
  var basePath = basePathParts.join('/');
  load.source = load.source
    .replace(templateUrlRegex, function(match, quote, url){
      let resolvedUrl = url;

      if (url.startsWith('.')) {
        resolvedUrl = basePath + url.substr(1);
      }

      return `templateUrl: '${resolvedUrl}'`;
    })
    .replace(stylesRegex, function(match, relativeUrls) {
      var urls = [];

      while ((match = stringRegex.exec(relativeUrls)) !== null) {
        if (match[2].startsWith('.')) {
          urls.push(`'${basePath.substr(1)}${match[2].substr(1)}'`);
        } else {
          urls.push(`'${match[2]}'`);
        }
      }

      return "styleUrls: [" + urls.join(', ') + "]";
    });

  return load;
};

Code taken from here: https://angular.io/resources/live-examples/forms/ts/eplnkr.html

0
votes

systemjs-angular-loader.js is a new module mentioned in the Change Log for March 13, https://angular.io/docs/ts/latest/guide/change-log.html, but I have yet to find it.

0
votes

The issue is pointed out in the first lines of your error log:

GET http://localhost:3000/systemjs-angular-loader.js 404 Not Found 2ms  zone.js (line 2019)

Which means systemjs-angular-loader.js file is missing in your project.

As @TimFogarty mentioned in his answer, systemjs-angular-loader.js is a new module added to Angular 4.0.

Since the official docs still don't shed light on what should be inside the systemjs-angular-loader.js and even Angular-CLI doesn't generate it, I believe the only option is to take it from the official Angular Quickstart project.

Just for reference, here is its current version:

var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;

module.exports.translate = function(load){
  if (load.source.indexOf('moduleId') != -1) return load;

  var url = document.createElement('a');
  url.href = load.address;

  var basePathParts = url.pathname.split('/');

  basePathParts.pop();
  var basePath = basePathParts.join('/');

  var baseHref = document.createElement('a');
  baseHref.href = this.baseURL;
  baseHref = baseHref.pathname;

  if (!baseHref.startsWith('/base/')) { // it is not karma
    basePath = basePath.replace(baseHref, '');
  }

  load.source = load.source
    .replace(templateUrlRegex, function(match, quote, url){
      var resolvedUrl = url;

      if (url.startsWith('.')) {
        resolvedUrl = basePath + url.substr(1);
      }

      return 'templateUrl: "' + resolvedUrl + '"';
    })
    .replace(stylesRegex, function(match, relativeUrls) {
      var urls = [];

      while ((match = stringRegex.exec(relativeUrls)) !== null) {
        if (match[2].startsWith('.')) {
          urls.push('"' + basePath + match[2].substr(1) + '"');
        } else {
          urls.push('"' + match[2] + '"');
        }
      }

      return "styleUrls: [" + urls.join(', ') + "]";
    });

  return load;
};
0
votes

I am using Angular 4.3.2, a recent but not the latest version, and I do not have systemjs-angular-loader.js in my package.json file. I do not get the OP's 404 error. Does this mean systemjs-angular-loader.js is now buried somewhere in version 4.x.y?

On the other hand, if I remove moduleId: module.id from the @Component in my app.component.ts, the page no longer loads. It feels like some code change has been made, hinted at in the documentation changelog https://devdocs.io/angular~2/guide/change-log, but not described in Angular Changelog https://github.com/angular/angular/blob/master/CHANGELOG.md or Getting Started documentation. There are no examples (before -> after) to be found in Angular documentation.

The answer needs to come in official Angular code documentation - with before -> after examples. Then reference it here.