angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Angular 6 SSR build issue

Open ggarg2 opened this issue 8 years ago • 39 comments

Hi Team,

I am new in AngularFire2 library. I am getting one issue while making ssr build only. If i am running ng run build:ssr then i am getting some warnings.

WARNING in ./node_modules/angularfire2/firebase.app.module.js 15:22-26 "export 'apps' was not found in 'firebase/app' @ ./node_modules/angularfire2/firebase.app.module.js @ ./node_modules/angularfire2/public_api.js @ ./node_modules/angularfire2/index.js @ ./dist/server/main.js @ ./server.ts

WARNING in ./node_modules/angularfire2/firebase.app.module.js 16:27-40 "export 'initializeApp' was not found in 'firebase/app' @ ./node_modules/angularfire2/firebase.app.module.js @ ./node_modules/angularfire2/public_api.js @ ./node_modules/angularfire2/index.js @ ./dist/server/main.js @ ./server.ts

But after running npm run serve:ssr, i am getting this error

TypeError: Cannot read property 'filter' of undefined at _firebaseAppFactory (E:\dev\migration\dist\server.js:206291:73) at E:\dev\migration\dist\server.js:210811:95 at ZoneDelegate.invoke (E:\dev\migration\dist\server.js:527:26) at Zone.run (E:\dev\migration\dist\server.js:277:43) at NgZone.runOutsideAngular (E:\dev\migration\dist\server.js:7898:28) at new AngularFireAuth (E:\dev\migration\dist\server.js:210810:26) at _createClass (E:\dev\migration\dist\server.js:13149:20) at _createProviderInstance$1 (E:\dev\migration\dist\server.js:13111:26) at resolveNgModuleDep (E:\dev\migration\dist\server.js:13077:21) at _createClass (E:\dev\migration\dist\server.js:13143:107)

Because of this error i am unable to migrate from Angular 5 to Angular 6. Please help

ggarg2 avatar Jun 03 '18 21:06 ggarg2

Hello @alexdabast Judging from what is working for others in this issue, my best guess is that it must be environment/package related.

I am not currently using Firebase Messaging. My issue was using Firebase with AngularFire2 in dynamic SSR and/or generative prerendering with Angular Universal.

My environment is:

Angular CLI: 6.1.0-beta.0
Node: 8.11.3
OS: win32 x64

Also from my last comment, you can pull the app I am working on in question from my public repository here: https://github.com/jwuliger/personal-website

As for why I need to import Firebase that way into AngularFire2 - well not 100% sure. My best guess is all the typing changes in vanilla Firebase. My work-around to patch AngularFire2 came from how the lib angularfire-lite was importing Firebase; as this lib did work with SSR.

There is an Angular v6 Branch you can check out here that also includes Firebase Messaging: https://github.com/hamedbaatour/angularfire-lite/tree/v6.0

I hope that this helps and be sure to checkout my repo to see AngularFire2 & Angular Universal working.

Next goal is to be able to run my node server from a cloud function whenever they fix that.

Thanks

jwuliger avatar Jul 09 '18 11:07 jwuliger

Thank you I made it work by downgrading to angularfire2 to rc9 so for me I think there is still something wrong in the rc10/11 release

alexdabast avatar Jul 09 '18 11:07 alexdabast

@alexdabast Yes, that was another solution. I wanted to use the latest RC.11 build though. I am glad you have everything squared away now!

jwuliger avatar Jul 09 '18 11:07 jwuliger

@jamesdaniels i'm getting this error with rc.11 as well any chance you could look into it again? See above for others with the same issue

ValentinFunk avatar Jul 10 '18 18:07 ValentinFunk

**Still the error comes in 5.0.0-rc.11 ** ERROR TypeError: Cannot read property 'filter' of undefined at _firebaseAppFactory (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:58665:41) at /Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:98786:66 at ZoneDelegate.module.exports.ZoneDelegate.invoke (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:156663:26) at Zone.module.exports.Zone.run (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:156413:43) at NgZone.module.exports.NgZone.runOutsideAngular (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:4071:28) at new AngularFirestore (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:98785:31) at _createClass (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8568:20) at _createProviderInstance (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8530:26) at resolveNgModuleDep (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8493:21) at _createClass (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8562:29) TypeError: Cannot read property 'filter' of undefined at _firebaseAppFactory (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:58665:41) at /Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:98786:66 at ZoneDelegate.module.exports.ZoneDelegate.invoke (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:156663:26) at Zone.module.exports.Zone.run (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:156413:43) at NgZone.module.exports.NgZone.runOutsideAngular (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:4071:28) at new AngularFirestore (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:98785:31) at _createClass (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8568:20) at _createProviderInstance (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8530:26) at resolveNgModuleDep (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8493:21) at _createClass (/Users/umeshblader/Projects/front-end/ecosoftlk-new/dist/server.js:8562

umeshblader3 avatar Jul 17 '18 04:07 umeshblader3

@jamesdaniels Still error, I think this should be reopened "angularfire2": "^5.0.0-rc.11", "firebase": "^5.2.0", "@angular": "^6.0.7", "@nguniversal": "^6.0.0",

ERROR { Error: Uncaught (in promise): TypeError: Cannot read property 'filter' of undefined
TypeError: Cannot read property 'filter' of undefined
    at _firebaseAppFactory (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:165705:73)
    at /Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:170755:95
    at ZoneDelegate.invoke (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:463:26)
    at Zone.run (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:213:43)

philip-firstorder avatar Jul 19 '18 04:07 philip-firstorder

Fixed it with "angularfire2": "^5.0.0-rc.9", but then I got this error from my prerenderer

> [email protected] generate:prerender /Users/philip_sultanescu/Projects/togaimperial.com
> cd dist && node prerender

/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:282
                        throw error;
                        ^

ReferenceError: XMLHttpRequest is not defined
    at Qc (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194013:1401)
    at T.g.fa (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194043:234)
    at Xc (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194015:266)
    at Vc (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194014:297)
    at ye.g.Qa (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194057:22)
    at Timeout.<anonymous> (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:194013:802)
    at ZoneDelegate.invokeTask (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:511:31)
    at Zone.runTask (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:278:47)
    at ZoneTask.invokeTask (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:586:34)
    at Timeout.ZoneTask.invoke (/Users/philip_sultanescu/Projects/togaimperial.com/dist/prerender.js:575:48)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] generate:prerender: `cd dist && node prerender`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] generate:prerender script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

I fixed this by adding these 2 lines from server.ts also in prerender.ts

// Required for Firebase
(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

philip-firstorder avatar Jul 26 '18 22:07 philip-firstorder

Also important is to call process.exit(); at the end of prerender.ts file in care you use firebase or firestore, otherwise the cd dist && node prerender will just wait forever because of the persistent db connections.

For prerendering with firebase there is a great article here: https://angularfirebase.com/lessons/angular-6-universal-ssr-prerendering-firebase-hosting/#Prerendering-vs-Server-Side-Rendering-vs-Rendertron

philip-firstorder avatar Jul 29 '18 00:07 philip-firstorder

I get this error using rc9 and rx11! I fixed it by using @jwuliger's fix.

davidpoxon avatar Jul 29 '18 02:07 davidpoxon

so what is the last decision? which versions of angularfire and firebase solves problem? or how to import firebase? still i got the error "firebase": "^5.0.4", "angularfire2": "^5.0.0-rc.11", "@angular/compiler": "^5.0.0",

TypeError: Cannot read property 'filter' of undefined at _firebaseAppFactory

gfaganli avatar Aug 08 '18 08:08 gfaganli

Got everything to work using: "firebase": "^5.3.0", "angularfire2": "^5.0.0-rc.9"

RudiPersson avatar Aug 08 '18 09:08 RudiPersson

still not working, now error switched to that one -_- : Error: The XMLHttpRequest compatibility library was not found.

i followed @philip-firstorder but again not working // Required for Firebase (global as any).WebSocket = require('ws'); (global as any).XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;

after all - Error: Unable to Instantiate Firebase Messaing ReferenceError: self is not defined

p.s ng serve works fine, SSR not working

gfaganli avatar Aug 08 '18 14:08 gfaganli

Same problem here...

BluebambooSRL avatar Aug 13 '18 19:08 BluebambooSRL

@gfaganli Here is my project for you to do a side-by-side comparison with yours, maybe it helps. Mine compiles for ssr, prerender, serve, everything...

https://github.com/firstorder-gmbh/firstorder.io

philip-firstorder avatar Aug 14 '18 09:08 philip-firstorder

Fixing the apps/filter issue in #1821

jamesdaniels avatar Aug 17 '18 22:08 jamesdaniels

Thanks @jamesdaniels! Universal doesn't throw that error with rc12 anymore. The XMLHttpRequest fix mentioned by @philip-firstorder is still required which is simple enough.

itsthesteve avatar Aug 24 '18 22:08 itsthesteve

Is latest version compatible with angular 5? I am using firebase in 2 project, one app is in the angular 5, another angular 6. Angular 6 works fine with "angularfire2": "^5.0.0-rc.12", "firebase": "^5.4.1". But in angular 5 it throws an error like - Unable to Instantiate Firebase Messaing TypeError: C.messaging is not a function Error comes from line in the below, but could not find any solution

try { this.messaging = firebase.messaging(); } catch (e) { console.log('Unable to Instantiate Firebase Messaing', e); }

gfaganli avatar Sep 07 '18 11:09 gfaganli

@gfaganli

Check these and see if it solves your issue:

  • https://github.com/angular/angularfire2/issues/1707#issuecomment-414947706
  • https://github.com/angular/angularfire2/issues/1707#issuecomment-415001581

ValentinFunk avatar Sep 07 '18 12:09 ValentinFunk

@Kamshak Thanks for your help. I tried first one bit that did not work. In order to try second solution i have to upgrade project to angular 6, which i would not prefer.

gfaganli avatar Sep 07 '18 13:09 gfaganli