Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack imported class from module is not a constructor

I have an angular2 package library ( call it A) that I import from another different angular2 project ( call it B ) using npm install .
All services , components from A seems to work on the B project , they are well imported injected and used .
But , only simple class definitions doesn't work . i.e I have a class defined as follow

export class JustAClass{
    public Title:string;
    constructor(title:string){
        this.Title = title;
    }

    getTitleLong():string{
        return 'Long '+this.Title;
    }
}

and in the index.d.ts I export it .

export { JustAClass } from './components/test/just';

At project B I can see the just.d.ts file as follow :

export declare class JustAClass {
    Title: string;
    constructor(title: string);
    getTitleLong(): any;
}

I work with webswtorm , when I use this class inside a file of project B it gives me intellisence and knows to point me to file definition :

import { JustAClass } from 'my-project-lib-A';

@Component({
    templateUrl: 'orders.html'
})
export class SomePage {
    

    constructor() {

        var d = new JustAClass('aaaa'); // this is undefiend in run time
        console.log(d.getTitleLong());

    }

I get it 'JustAClass' undefined , and webpack throws an error :

WEBPACK_IMPORTED_MODULE_2_my-project-lib-A.JustAClass is not a constructor

I'm using ionic2 as project B if it matters .

like image 821
Haddar Macdasi Avatar asked Feb 14 '26 15:02

Haddar Macdasi


1 Answers

If somebody stumbles upon this, one of the possible solutions that worked for me was to remove cyclic dependency between imported class and importer. It seems that webpack resolves cyclic dependencies by replacing imported class with undefined or some similar construct which then causes runtime exception.

like image 98
Martin K. Avatar answered Feb 17 '26 11:02

Martin K.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!