Typescript, Firebase, Cordova: Cannot find module for 'firebase'

Davide R

I'm developing an hybrid app with Cordova and Typescript. I need to integrate Firebase in order to read/update/save some datas from database.

My issue is about importing and using Firebase in Typescript (no framework used). And also to test it on localhost.

So, this is what I have:

webpack.config.js

module.exports = function(options) {
  return {
    [...]
    resolve: {
      extensions: ['.ts', '.js'],
      alias: {
        firebase: helpers.root('node_modules/firebase/firebase.js')
      }
    },
    [...]
    module: {
      rules: [
        { test: /\.ts$/, enforce: 'pre', loader: 'tslint-loader' },
        { test: /firebase\.js$/, loader: 'expose-loader?firebase' },
        { test: /\.ts$/, loader: 'ts-loader', exclude: '/node_modules/' },
      ]
    }
  }
};

package.json

{
  [...]
  "main": "src/ts/index.ts",
  "devDependencies": {
    [...]
    "typescript": "2.2.2",
    "firebase": "^4.6.0"
  },
  "dependencies": {
    "@types/firebase": "^2.4.32",
    [...]
  }
}

tsconfig.json

{
  "files": [
    "./src/ts/index.ts",
    "./node_modules/@types/firebase/index.d.ts"
  ]
}

src/ts/index.ts

import * as firebase from "firebase";

export namespace Application {

  export function initialize() {
    const db = firebase.database();
  }

}

window.onload = function() {
  Application.initialize();
};

When I launch the app, it throws an error in console:

Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_2_firebase__ is not a constructor

Please, can you help me?

UPDATE: I've deleted unuseful lines of code and, as @kevinSpaceyIsKeyserSöze told me, I've edited part of Firebase code.

Davide R

After a long documentation and research and a few tries, this is a working solution:

First of all, you must have typings installed. Open the shell and type:

  1. typings install github:typed-typings/npm-firebase
  2. typings install firebase
  3. npm i --save-dev firebase

Then in tsconfig.json, under files object:

"files": [
  "./typings/index.d.ts"
]

In src/ts/index.ts:

import * as firebase from "firebase";

export namespace Application {

  export function initialize() {
    const firebaseApp = firebase.initializeApp({ ...firebase config });
    const db = firebaseApp.database();
  }

}

window.onload = function() {
  Application.initialize();
};

That's all!

In this way, there won't be errors of Cannot find module for 'firebase'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Firebase cannot find 'module <Firebase>'

Cannot find module 'firebase' or its corresponding type declarations. React + TypeScript + Firebase

After Firebase deploy - Error: cannot find module

Cannot find module 'firebase-admin'

Uncaught Error: Cannot find module 'firebase'

Cannot find name 'firebase' vanilla typescript

Angular Firebase Function Deploy Error: Cannot find module 'firebase/app'

FIrebase deploy error: Cannot find module 'firebase-admin'

Ionic Cordova Build prod: Cannot find module “.” - Typescript version >3

Cannot deploy Firebase Cloud Functions [Cannot find module 'protobufjs/minimal']

AngularFire2 - Cannot find module @firebase/database

Firebase Admin Nodejs cannot find module service_account.json

Error: Error parsing triggers: Cannot find module 'firebase-functions'

Error: "Cannot find module" while running firebase cloud function

Firebase CLI console: Cannot find module @google-cloud/tasks

Cannot find module '@firebase/app-types/private'

Node.js error: Cannot find module 'firebase/storage'

Error: Error parsing triggers: Cannot find module 'firebase-functions'.?

Cannot find getValue in firebase

Firebase - Node Cloud Functions Error parsing triggers: Cannot find module 'firebase-functions'

Updating firebase-functions from 3.19.0 to 4.3.1 leads to `Cannot find module 'firebase-admin/auth'`

Typescript cannot find module for stripe

typescript: Cannot find module 'react'

LernaJS Typescript cannot find module

Typescript cannot find module Protractor

TypeScript cannot find module error

NodeJS, Typescript: Cannot find module

Typescript - Importing a Module - Cannot Find Module

`Cannot find module` for my own TypeScript module