-1

I want to use Firebase Admin SDK to generate Firebase auth tokens, when i set Admin SDK configuration in my js file, the following error appears (Uncaught ReferenceError: require is not defined)

How i can require firebase-admin and serviceAccountKey json file in my js file

the project was developed using laravel framework.

I tried to use REQUIREJS API to require service account kye , but the following error appear >> Uncaught SyntaxError: Unexpected token :

var admin = require(['firebase-admin']);

var serviceAccount = require("{{ asset('/serviceAccountKey.json') }}");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: ""
});

Uncaught ReferenceError: require is not defined

Uncaught SyntaxError: Unexpected token :

Sam
  • 15
  • 2
  • 9

2 Answers2

0

You might have a typo, try to remove the square brackets:

var admin = require('firebase-admin');

or try:

import * as admin from 'firebase-admin';

also make sure you added the the package.json with firebase-amin in it

If you don't already have a package.json file, create one via npm init. Next, install the firebase-admin npm package and save it to your package.json:

if you don't have it create one using the command

npm install firebase-admin --save

Source

aldobaie
  • 1,387
  • 10
  • 15
  • Uncaught SyntaxError: Unexpected token * – Sam Aug 26 '19 at 12:25
  • I use typescript with Firebase Functions and this works for me: import functions = require('firebase-functions'); import admin = require('firebase-admin'); admin.initializeApp(); – aldobaie Aug 26 '19 at 15:31
0
const firebaseAdmin = require('firebase-admin');
const config = require('../config');
const catchError = require('http-errors');

const serviceAccount = require(config.FIREBASE_KEY);
firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: config.FIREBASE_DB
});

install the firebase-admin dependencies with npm install --save firebase-admin

edit

the config file contains everything related to the firebase configurations hence you can gitignore it easily

Edit

I find this link that may be helpful and may not be related to anything on firebase.

HexaCrop
  • 3,863
  • 2
  • 23
  • 50