angulartypescriptrxjssystemjs

Unhandled Promise rejection: Rx_1.Subject is not a constructor ; Zone: ; Task: Promise.then ; Value: TypeError: Rx_1.Subject is not a constructor


I have an Angular 2 app, which uses systemjs to map/load.

Importing the rxjs in the project, and referencing it is fine, but when the code is deployed, it fails to construct a Subject variable with message:

"Unhandled Promise rejection: Rx_1.Subject is not a constructor ; Zone: ; Task: Promise.then ; Value: TypeError: Rx_1.Subject is not a constructor".

Rx on systemjs:

(function (global) {
var map = {
    'app': 'app',  
    '@angular': 'js/@angular', 
     // other libraries
    'rxjs': 'js/rxjs', 
};

// packages tells the System loader which filename and/or extensions to look for by default (when none are specified) 

var packages = {
    'app': { main: 'main.js', defaultExtension: 'js' },
    'rxjs': { defaultExtension: 'js' }
};

The way I am instantiating the subject:

import { Injectable } from '@angular/core';
import { Subject } from "rxjs/Rx";

@Injectable()
export class MyService {

constructor(private http: Http) {
    let b = new Subject<string>(); // exteption HERE
    let m = b.asObservable(); 
    b.next("k");

all of rxjs is loaded fine in the browser.


UPDATE: Looking at this more, if I import Rx in my Angular service as:

import * as Rx from 'rxjs/Rx';

and than inspect the Rx object in the console, it just has a default, and __useDefault property getter. Everything else, Observable, Subject is accessible through them.

Like doing:

var observable = Rx.Observable.create(function (observer) {
        observer.next(1);
    });

gets back an:

VM1083:1 Uncaught TypeError: Cannot read property 'create' of undefined

whereas, of course:

var observable = Rx.default.Observable.create(function (observer) {
        observer.next(1);
    });

works fine.

My project is in TypeScript, I have tried target compilation ES5 and ES6.


Solution

  • I managed to get unblocked after looking at How to import `Observable` from `Rx` (not angular)

    I changed my tsconfig.json module from "system" to "commonjs", and Rx lib files are being transpiled without getters.