TypeError: Cannot read property 'firebase' of undefined in NativeScript app

I'm creating a mobile app by using NativeScript with AngularJS2 & TypeScript. For using Firebase as DB to store data.

For using firebase in NativeScript I'm suing the following library -

Here is my code -

import {Component} from "@angular/core";
import {User} from "./shared/user/user";
import {UserService} from "./shared/user/user.service";
import {HTTP_PROVIDERS} from "@angular/http";
var firebase = require("nativescript-plugin-firebase");

@Component({
  selector: "my-app",
  providers: [UserService, HTTP_PROVIDERS],
  templateUrl: "pages/login/login.html",
  styleUrls: ["pages/login/login-common.css", "pages/login/login.css"]
})

export class AppComponent {
    user: User;
    isLoggingIn = true;

    constructor(private _userService: UserService) {
        this.user = new User();
    }

    submit() {
        if (this.isLoggingIn) {
            this.login();
        } else {
            this.signUp();
        }
    }

    login() {
        // TODO: Define
        console.log('Clicked on Login button');

        firebase.init({
            persist: true // Allow disk persistence. Default false.
        }).then(
            function (instance) {
                console.log("firebase.init done");
            },
            function (error) {
                console.log("firebase.init error: " + error);
            }
        );
    }

    signUp() {
        this._userService.register(this.user);
    }

    toggleDisplay() {
        this.isLoggingIn = !this.isLoggingIn;
    }
}

Method "Login()" is getting invoked when a user clicks on "Sign in" button. I've pasted that firebase code for checking everything is fine with firebase or not.

But, whenever that "Login()" is invoked it's showing following error.

JS: Clicked on Login button
JS: Error in firebase.init: TypeError: Cannot read property 'firebase' of undefined
JS: firebase.init error: TypeError: Cannot read property 'firebase' of undefined

I can understand this error is showing because I'm not properly inserting that "firebase" dependency inside the class. But, as I'm new to TypeScript I can't able to understand.. how to do it.

Need some help

Edited -

Getting following error from VS Code when using import * as firebase from "nativescript-plugin-firebase"; rather than var firebase = require("nativescript-plugin-firebase");

enter image description here


ANSWERS:


import firebase = require("nativescript-plugin-firebase"); in reference.d.ts add /// <reference path="./node_modules/nativescript-plugin-firebase/firebase" /> (without extensions)



 MORE:


 ? firebase onAuth() throwing: TypeError cannot read property 'auth' of undefined
 ? Cannot read property firebase of undefined error
 ? Accessing element height in NativeScript
 ? Nativescript canvas
 ? Is there localstorage in nativescript?
 ? $index not working in Nativescript
 ? In NativeScript with Angular2, how can I get an interface element the id?
 ? In NativeScript with Angular2, how can I get an interface element the id?
 ? In NativeScript with Angular2, how can I get an interface element the id?
 ? In NativeScript with Angular2 get element value