0

I am trying to use my NodeJS server to perform some queries along with serving my Angular app, but some weird things are going on, when I add any firebase command...

I have no idea why this version of app works without any errors:

const express = require('express');
const admin = require('firebase-admin')
const path = require('path')
const app = express();

const distDir = __dirname + "/dist/my-firebase-admin-app";
const serviceAccount = require('./admin-sdk-secret.json');

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


app.use(express.static(distDir));
app.use('*', (req, res) => {
  res.sendFile(path.resolve(distDir +'index.html'));
});

app.listen(process.env.PORT || 8080);

but this crashes after a few seconds:

const express = require('express');
const admin = require('firebase-admin')
const path = require('path')
const app = express();

const distDir = __dirname + "/dist/my-firebase-admin-app";
const serviceAccount = require('./admin-sdk-secret.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "...myURL..."
});
admin.database().ref('/admins');

app.use(express.static(distDir));
app.use('*', (req, res) => {
  res.sendFile(path.resolve(distDir +'index.html'));
});

app.listen(process.env.PORT || 8080);

The only thing I add is just firebase ref. What is wrong?

Alex Teexone
  • 63
  • 1
  • 5
  • it crashes with no errors on console? – yaya Aug 23 '20 at 11:33
  • yes, just shuts down. if i use nodemon, it just says: "app crashed, waiting..." – Alex Teexone Aug 23 '20 at 11:34
  • try : `console.log(admin.database())` instead to see if the problem is with `.database()` or `.ref('/admins')` – yaya Aug 23 '20 at 11:37
  • it returned a Database object and just after this it have crashed – Alex Teexone Aug 23 '20 at 11:42
  • 1
    i reproduced this code and the problem not exists in my enviroment. also you have a bug: `distDir +'index.html'` should be: `distDir +'/index.html'`. it causes the app to crash and it says: `no such file or directory`. if it didn't fix it, you can clone my repo: https://github.com/ya3ya6/stack-play-angularfirebase and check if it works for you, and try to compare it with your project to find the bug. – yaya Aug 23 '20 at 12:59
  • I was managed to determine, why my environment crashes. server.js crashes with message: "Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail". After an hour spent on searching for a solution, I did not find anything useful. Could you help me, if that is possible? – Alex Teexone Aug 23 '20 at 14:57
  • if you can share the code or simplifed version of it, i can try to debug it. but without having any code, it's so hard to debug it. (can't you put on github?) – yaya Aug 23 '20 at 15:02
  • Actually, it is the whole code I have. Just server.js. I want to implement querying to database via http request, like ```http://myproj.net/q?qtype=0&uid=1234```. So I am only using this server.js, which is given above – Alex Teexone Aug 23 '20 at 15:11
  • can you test this repo: https://github.com/ya3ya6/stack-play-angularfirebase does it work on your enviroment? – yaya Aug 23 '20 at 15:15
  • Yes, it is working, but this does not: https://github.com/teexone/broken-app-config – Alex Teexone Aug 23 '20 at 15:40
  • i checked your repo, you didn't include your `graph-builder-2bd85-firebase-adminsdk-f6vis-9005e2081b.json` file, so i used mine instead. and it's working without any crash or warning. so: 1. replace your `.json` file with mine (https://github.com/ya3ya6/stack-play-angularfirebase/blob/master/stackplay2-firebase-adminsdk-c54zb-f495f59560.json) and check if it works (don't forget to rename it on `server.js`). 2. if it's possible upload your json file to github , so i can check it. – yaya Aug 23 '20 at 16:14
  • It seems like that something is wrong in my env, because your json doesn't work either. My json looks the same, I have just downloaded it, and rechecked - nothing. – Alex Teexone Aug 23 '20 at 17:17
  • I have just tried to implement query to my db from your files and it is worked. But if I copy the same thing into my server.js, it just crashes – Alex Teexone Aug 23 '20 at 17:47
  • create a new empty project with `npm init -y`, then install dependencies with : `npm install firebase-admin express`, then put three files in it: `index.js`, `stackplay2-firebase-adminsdk-c54zb-f495f59560.json` and `dist/my-firebase-admin-app/index.html` (from my repo) . then run it with : `node index`. if it works, it means that something is wrong with your project, not your enviroment. if it didn't work, it means that something is wrong with your enviroment (perhaps nodejs version. i'm using `node 10`, not `node 12`). (also are you in production, or on your local dev enviroment?) – yaya Aug 23 '20 at 18:10
  • and also try to catch the error on the line who causes the server to shutdown : `try{admin.database().ref('/admins');}catch(e){console.log(error, e); return res.send('err')}` – yaya Aug 23 '20 at 18:27
  • no errors are caught. seems like problem with my project. could you check it please? I have updated broken-app-config repo with the whole project – Alex Teexone Aug 24 '20 at 06:23
  • it works fine for me. i would suggest: download your repo, put it on a root path (like: `c://myapp`), remove `node` from dependencies, run `npm install` and then `npm start`. if will probably work but if it didn't work, remove `engines`, `devDependencies`, `scripts` section from `package.json`, and remove all dependencies except `express` and `firebase-admin`. then it'll probably work but if didn't, replace `package.json` with `package.json` from another project you created. these steps are step to step to becomming close to your working project , so you'll understand what's the problem. – yaya Aug 24 '20 at 08:34
  • now i reproduced the issue. i get the crash when i add the firebase line. but only when i run `npm start`, not when i run `node server.js`. i'll try to find the problem. – yaya Aug 24 '20 at 09:10
  • ok, i found the issue. there is a conflict problem in your package.lock.json. after cloning your repo, if i don't remove the `package.lock.json`, and the i do `npm i`, it will cause the app to crash. but i cloned it another time, i removed the `package.lock.json`, and then `npm i`, then it works without any error. also note that your current project has the `nodemodules` folder (unlike on github), so you should remove both of `package.lock` and `nodemodules` folders, then `npm i`. also if you're on a team, you should care about other members as well. here is a link about it : – yaya Aug 24 '20 at 11:28
  • here is the link about concerns about removing `package.lock.json` for other team members: https://stackoverflow.com/questions/54124033/deleting-package-lock-json-to-resolve-conflicts-quickly/54127283 – yaya Aug 24 '20 at 11:29
  • 1
    Thank you so much, it is worked and now everything is fine. – Alex Teexone Aug 24 '20 at 15:18
  • Hi @AlexTeexone can I know what worked for you? I am in exactly similar issue. – Abhijeet Oct 25 '20 at 08:00

0 Answers0