0
votes

I'm working on project where I'm using express in firebase functions to run server side rendering pages using handlebars, Everything working perfectly but when I use firestore admin sdk I get this error:

SyntaxError: Invalid or unexpected token
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)

I'm using this code in my functions/index.js file

const functions = require('firebase-functions');
const express = require('express')
const engines = require('consolidate')
const cookieParser = require('cookie-parser')
const serviceAccount = require('./lendme360-firebase-adminsdk-easkh-d9130c0494.json');
var admin = require('firebase-admin')
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
})

const stripe = require('stripe')('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
const db = admin.firestore()
const app = express()
app.engine('hbs', engines.handlebars)
app.set('views', './views')
app.use(cookieParser())
app.set('view engine', 'hbs')
...
...
...
...
...
exports.app = functions.https.onRequest(app)

Function is working properly but when try to const db = admin.firestore() gives me error I also tried emulators for both functions and firestore and still getting the same error

Please help me What I'm doing wrong here

Thanks in advance

1
What is the error message?Jay Codist

1 Answers

0
votes

I could reproduce this issue, it seems that the module is not loaded in your environment. I could notice that you mentioned that you used other environments to test this.

In a local environment try to run the a test in which you just initialize the module with "const db = admin.firestore()" you should get the same error as before, but they I would ask to run "npm install firebase-admin --save" as per this documentation, this gets the module to be used with node js.

In firebase functions you should have defined your modules at your package.json as per this other documentation, this should solve the error message that is telling us that node is not recognizing the syntax or the command from the module (as the module is not loaded).