Build failed: npm ERR! Cannot read property 'firebase-admin' of undefined - firebase

I am trying to update a Firebase Cloud Function that I havent touched for a year. Deployment went without issues back then.
Here is my setup: I have a root index.ts file, and functions are organized into folders.
root index.ts
import {initializeApp} from "firebase-admin";
// Initialize Firebase Admin
initializeApp();
export {notifyNewMessages} from "./notify_new_messages";
export {onUpdateUsers, onUpdateUserRatings} from "./on_update/index";
export {onUninstall} from "./on_uninstall/index";
and notifyNewMessages file starts like this
import {FirebaseError, firestore, messaging} from "firebase-admin";
import * as functions from "firebase-functions";
import i18n from "./localization";
import {Ask, Device, User} from "./models";
import DocumentSnapshot = firestore.DocumentSnapshot;
const fcm: messaging.Messaging = messaging();
Could this separated root file cause this problem?
Cheers,

It looks like you are using ES Modules, and as firebase has moved to modular SDK namespace versions require some modifications so try the following:
import * as admin from 'firebase-admin';
const app: admin.app.App = admin.initializeApp();
You can go through this docs for other changes to be made.

Related

How to import firebase storage in web? I got an error everytime I run it. I already got the dependencies in my pubspec

I got it my pubscpec and I import it whenever I used it like this:
import 'package:firebase_storage/firebase_storage.dart';
FirebaseStorage fs = FirebaseStorage.instance;
This is the error I got. I need to upload image

/src/firebase.js Module not found: Can't resolve 'firebase' in '/Users/anujgoyal/Desktop/reels/src'\?

image of the project and files
I am trying to import firebase in my react app, using the following syntax:
import firebase from "firebase";
But I am facing following issue:
./src/firebase.js
Module not found: Can't resolve 'firebase' in '/Users/Desktop/reels/src'
commands i have ran on terminal:
npm i firebase
Also i have checked in the node modules firebase was there.
You should try this code
import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
I think you're using the ^9 version, so try this:
import firebase from 'firebase/compat/app'

How can I import firebase-database as an es6 module

I want to import the firebase-database using esm import. I can only find the script version:
<script src="https://www.gstatic.com/firebasejs/7.21.0/firebase-database.js"></script>
What is the url I need for the esm module version?
Note: I do not want a "bare import", I am not using webpack etc. I need a complete url.
There is an older version available on unpkg.com but not the current version.
It turns out there are two CDN's that can provide this: snowpack/skypack, and jspm:
skypack:
import firebase from 'https://cdn.skypack.dev/#firebase/app'
import 'https://cdn.skypack.dev/#firebase/database'
jspm:
import { firebase } from 'https://jspm.dev/#firebase/app'
import 'https://jspm.dev/#firebase/database'
These both deal with "bare import" conversion to JS, and any code conversions required to be JS Modules.
Google does not appear to want to support a module form on their firebase CDN, an Issue to that effect was immediately closed, suggesting complex workflow solutions.
I'm really thankful to the two projects above, supporting simple es6 JS and zero workflow solutions.
Edit: snowpack's esinstall can turn most js files into modules. Here's a node script that brought in all my dependencies:
#!/usr/bin/env node --require esm
import { install } from 'esinstall'
import nodePolyfills from 'rollup-plugin-node-polyfills'
async function run() {
const foo = await install(
[
'mapbox-gl',
'three',
'three/examples/jsm/controls/OrbitControls.js',
'three/src/core/Object3D.js',
'chart.js',
'dat.gui',
'fflate',
'#turf/turf',
'stats.js',
'#firebase/app',
'#firebase/database',
],
{
rollup: {
plugins: [nodePolyfills()],
},
}
)
}
run()
If you're using a bundler, then follow the instructions in the documentation:
Install the firebase npm package and save it to your package.json file
by running:
npm install --save firebase
To include only specific Firebase products (like Authentication and
Cloud Firestore), import Firebase modules:
// Firebase App (the core Firebase SDK) is always required and must be listed first
import * as firebase from "firebase/app";
// If you enabled Analytics in your project, add the Firebase SDK for Analytics
import "firebase/analytics";
// Add the Firebase products that you want to use
import "firebase/auth";
import "firebase/firestore";
For realtime database, you would add import "firebase/database".

The basic installation of firebase package in WIX project doesn't work

I installed firebase v-5.8.2 package through WIX's package manager. The installation process doesn't have any problem per se. After installation, I added "import * as firebase from 'firebase/app'", without any other code. When I run the project, it gave error message "can not find module 'fs'"
In the wix's forum, seems that someone had success experience with an earlier firebase version. Wondering if the v-5.8.2 has some problem with WIX.
import * as firebase from 'firebase/app'
"can not find module 'fs'"
You are importing it on a backend .jsw file, right?
NPM modules can only be imported on backend files and not on your page code.
You can export the functions from a backend file like below:
//serverSide.jsw
import firebase from 'firebase';
var app = require('firebase');
export function backendModule() {
//Your backend function code here
}
Then import the function on the frontend page code like below:
import {backendModule} from 'backend/serverSide.jsw';
$w.onReady(function () {
//TODO: write your page related code here...
});
If you are already importing on the backend file then try this below
import firebase from 'firebase';

importing firebase to react-native

I'm trying to use the firebase in a react native project
I installed the firebase in my project:
npm install --save firebase
and I'm importing it into my app.js
import firebase from 'firebase';
import React, { Component } from 'react';
import {
Platform,
Text,
View
} from 'react-native';
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View >
<Text>Testando Firebase</Text>
</View>
);
}
}
and then the error is displayed:
Objects are not valida as a react child (found: object with
keys{&&typeof, type, key, ref, props, _owner, _store}). If you meant
to render a collection of children, use an array instead.
but when I remove the import from firebase, the app works perfectly.
Try to install the version 5.0.2. Use this command as administrator:
npm uninstall firebase
npm install --save firebase#5.0.2
As far as i see, the current version (5.0.4) is buggy!
You need to import as follow
import * as firebase from 'firebase';
You first must have firebase installed.
The solution is to uninstall the current version of Firebase and install an older version.
1st step npm uninstall firebase
2nd step npm i firebase#8.0.3
It was the version that worked for my case.

Resources