Firebase:Cannot find the firebase namespace; be sure to include firebase-app.js before this library. 我得到了这个错误。我应该如何修复我的代码?

问题描述 投票:0回答:1

当我用 "firebase deploy "来构建auth函数时,我得到了这样的错误信息,即 Cannot find the firebase namespace; be sure to include firebase-app.js before this library.

然后我试图修复,因为我认为我还没有成功地要求的 firebase我做了 firebase deploy 之后 npm install --save firebase.我可以看到 firebase但是,我无法成功部署。

我想解决这个错误。在哪里可以找到 firebase spacename 指的是?

此为错误

Error: Error occurred while parsing your function triggers.

Error: Cannot find the firebase namespace; be sure to include firebase-app.js before this library.
    at /Users/boutakuma/Desktop/socialapp/firebase_function/functions/node_modules/@firebase/auth/dist/auth.js:430:352
    at /Users/boutakuma/Desktop/socialapp/firebase_function/functions/node_modules/@firebase/auth/dist/auth.js:430:455
    at Object.<anonymous> (/Users/boutakuma/Desktop/socialapp/firebase_function/functions/node_modules/@firebase/auth/dist/auth.js:430:461)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)

这是我的index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');

const express = require('express');
const app = express();

admin.initializeApp();

var firebaseConfig = {
    apiKey: "***********************",
    authDomain: "************************",
    databaseURL: "************************",
    projectId: "************************",
    storageBucket: "************************",
    messagingSenderId: "************************",
    appId: "************************",
    measurementId: "************************"
  };

// require firebase
const firebase = require('firebase');
firebase.initializeApp(firebaseConfig);

app.get('/screams',(req,res) => {
    admin
    .firestore()
    .collection('screams')
    .orderBy('createdAt','descs')
    .get()
     .then((data) => {
         let screams = [];
         data.forEach((doc) => {
             screams.push({
                 screamId: doc.id,
                 body: doc.data().body,
                 userHandle: doc.data().userHandle,
                 createdAt: doc.data().createdAt, 
             });
         });
         return res.json(screams);
     })
     .catch(err => console.error(err));
})


app.post('/scream',(req,res) => {
    const newScream = {
        body: req.body.body,
        userHandle: req.body.userHandle,
        createdAt: new Date().toISOString()
    };
    admin.firestore()
     .collection('screams')
     .add(newScream)
     .then(doc => {
         res.json({ message: `document ${doc.id} created successfully` });
     })
     .catch(err => {
         res.status(500).json({ error: ' somethihg went wrong '});
         console.error(err);
     });
});

// Signup route

app.post('/signup', (req,res)=> {
    const newUser = {
        email: req.body.email,
        password: req.body.password,
        confirmPassword: req.body.confirmPassword,
        handle: req.body.handle,
    };

    //Todo balidate data
    firebase
    .auth()
    .createUserWithEmailAndPassword(newUser.email,newUser.password)
    .then((data) => {
        return res.status(201).json({ message: `user ${data.user.uid} signed up successgfully` });
    })
    .catch((err) => {
        console.error(err);
        return res.status(500).json({ error: err.code });
    });
});

exports.api = functions.https.onRequest(app);

目录构造

.
└── functions
    └── node_modules
        :
        ├── extend
        ├── fast-text-encoding
        ├── faye-websocket
        ├── finalhandler
        ├── firebase
        ├── firebase-admin
        ├── firebase-functions
        ├── form-data
        ├── forwarded
        :

补编。当我做 firebase deploy 在添加auth函数或require firebase之前,这段代码还能用。我想可能是周围的 auth or require('firebase');

我应该如何修复我的代码? 你认为是什么原因?

开发版本

Node.js: v10.15.3

Firebase : 7.15.0

firebase-admin.com:7.15.0 7.0.0

firebase-functions:3.6.2 3.6.2

node.js firebase firebase-authentication command
1个回答
1
投票

我可以解决这个问题!我发现原因是围绕firebase版本有关,所以,我做了以下工作。

  1. 我做了降级 npm install --save [email protected]
  2. 我写道 require('firebase') 在我的js表上并保存。
  3. 我做了 firebase deploy↓我得到了 Deploy complete!

参考来源

https:/github.comfirebasefirebase-toolsissues609。

© www.soinside.com 2019 - 2024. All rights reserved.