Angular TypeScript Error : traceur 404 not found

2019-09-14 22:37发布

问题:

  1. I am using the latest (4.x.x) angular quickstart from angular.io.
  2. I want to import firebase and angularfire2.

I imported the libraries in systemjs.config.js as :

map: {
    ...
    'firebase': 'npm:firebase',
    'angularfire2': 'npm:angularfire2/bundles/angularfire2.umd.js',
    'angularfire2/auth': 'npm:angularfire2/auth',
    'angularfire2/database': 'npm:angularfire2/database',
    ...
},
packages: {
    app: {
        defaultExtension: 'js',
        meta: {
            './*.js': {
                loader: 'systemjs-angular-loader.js'
            }
        }
    },
    'firebase':{
        main: './firebase.js',
        defaultExtension: 'js'
    },
    'angularfire2/database':{
        main: './database.js',
        defaultExtension: 'js'
    },
    'angularfire2/auth':{
        main: './auth.js',
        defaultExtension: 'js'
    },
}

Error Screen Cap

Update

I just want to emphasize this error

Unable to load transpiler to transpile http://localhost:3000/node_modules/angularfire2/database/database.js Error loading http://localhost:3000/node_modules/angularfire2/database/database.js as "angularfire2/database" from http://localhost:3000/app/app.module.js

回答1:

Ok, since angular developers keep changing their setup here are current step-by-step instructions how to load angularfire2 database module with angular 4 quickstart example.

start with cloning official quickstart repo

git clone https://github.com/angular/quickstart.git quickstart

install additional dependencies

npm i --save-dev angularfire2 firebase promise-polyfill plugin-typescript@6.0.4

change tsconfig.json to use "module": "system"

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

add necessary stuff to systemjs.config.js

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    transpiler: 'ts',
    typescriptOptions: {
      tsconfig: true
    },
    meta: {
      'typescript': {
        "exports": "ts"
      }
    },
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // 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.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',

      'angularfire2': 'npm:angularfire2',
      'ts':                         'npm:plugin-typescript',
      'typescript':                 'npm:typescript/lib/typescript.js',
      'firebase':                   'npm:firebase',
      'promise-polyfill':           'npm:promise-polyfill/promise.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: {
        main: './main.ts',
        defaultExtension: 'ts',
        meta: {
          './*.ts': {
            loader: 'systemjs-angular-loader.js'
          }
        }
      },
      rxjs: {
        defaultExtension: 'js'
      },
      angularfire2: {
        main: 'angularfire2.js',
        defaultExtension: 'js'
      },
      firebase: {
        defaultExtension: 'js'
      },
      ts: {
        main: 'lib/plugin.js',
        defaultExtension: 'js'
      }

    }
  });
})(this);

add AngularFireModule and AngularFireDatabaseModule to app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { AppComponent }   from './app.component';

@NgModule({
  imports:      [
    BrowserModule,
    AngularFireModule.initializeApp({
// NOTE: USE YOUR OWN API KEY
      apiKey: "AIzaSyDFbuKX0UeXje-PRAvwIymYo2jk-uGqMa4",
      authDomain: "test-bc800.firebaseapp.com",
      databaseURL: "https://test-bc800.firebaseio.com",
      storageBucket: ""
    }),
    AngularFireDatabaseModule
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})

export class AppModule { }

use db in app.component.ts

import { Component } from '@angular/core';
import { AngularFireDatabase } from 'angularfire2/database';
import { Subject } from 'rxjs/Subject';

@Component({
  selector: 'my-app',
  template: '<h1>Angularfire2 example app</h1>'
})
export class AppComponent {


  constructor(db: AngularFireDatabase) {
    // use db here
  }

}

Note that while this example works, it compiles angularfire source code from es6 to javascript in the browser. It seems like pre-bundled angularfire code was not installed with npm i angularfire2, so you will have to bundle it yourself for production.