question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular SSR - Error: Cannot find module '@firebase/app'

See original GitHub issue

Version info

Angular: 13.12 Firebase: 8.10.0 AngularFire: 6.1.5

Other (e.g. Ionic/Cordova, Node, browser, operating system): Node

I wanted an angular SSR app, but for some reason I always get a bug after the build. I don’t want to install anything on the server side.

Thank you for your help!

server.ts

import 'zone.js/node';
import 'zone.js/dist/zone-patch-rxjs';

import {ngExpressEngine} from '@nguniversal/express-engine';
import * as express from 'express';
import {join} from 'path';
import * as redis from 'redis';
import * as morgan from 'morgan';

import { enableProdMode } from '@angular/core';
import {AppServerModule} from './src/main.server';
import {APP_BASE_HREF} from '@angular/common';
import {existsSync} from 'fs';
import {REQUEST, RESPONSE} from '@nguniversal/express-engine/tokens';
import {environment} from './src/environments/environment';

// Polyfills required for Firebase
(global as any).WebSocket = require('ws');
(global as any).XMLHttpRequest = require('xhr2');

// Faster renders in prod mode
enableProdMode();

export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), environment.production ? 'browser' : 'dist/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  const redisClient = redis.createClient({
    host: environment.redis.host,
    db: environment.redis.db,
    auth_pass: environment.redis.auth_pass,
    prefix: environment.redis.prefix,
  });
  const cacheKey: (req: express.Request) => string = (req) => `:ssr:${req.originalUrl}`;


  server.engine('html', (_, options: any, callback) =>
    ngExpressEngine({
      bootstrap: AppServerModule,
      providers: [
        {provide: REQUEST, useValue: options.req},
        {provide: RESPONSE, useValue: options.req.res},
      ],
    })(_, options, callback)
  );

  server.set('view engine', 'html');
  server.set('views', distFolder);

  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));


  // All defend guarded page
  server.get('/admin*', (req, res) => {
    res.sendFile(join(distFolder, 'index.html'));
  });
  server.get('/profil*', (req, res) => {
    res.sendFile(join(distFolder, 'index.html'));
  });
  server.get('/penztar*', (req, res) => {
    res.sendFile(join(distFolder, 'index.html'));
  });


  // sitemap SEO
  server.get('/assets/sitemap/gallery.xml', (req, res) => {
    res.redirect(301, environment.production ? 'https://us-central1-konigs-art-kft.cloudfunctions.net/sitemap/assets/sitemap/gallery.xml' : 'https://us-central1-konigs-art-kft-dev.cloudfunctions.net/sitemap/assets/sitemap/gallery.xml');
  });
  server.get('/assets/sitemap/blogs.xml', (req, res) => {
    res.redirect(301, environment.production ? 'https://us-central1-konigs-art-kft.cloudfunctions.net/sitemap/assets/sitemap/blogs.xml' : 'https://us-central1-konigs-art-kft-dev.cloudfunctions.net/sitemap/assets/sitemap/blogs.xml');
  });
  server.get('/assets/sitemap/products.xml', (req, res) => {
    res.redirect(301, environment.production ? 'https://us-central1-konigs-art-kft.cloudfunctions.net/sitemap/assets/sitemap/products.xml' : 'https://us-central1-konigs-art-kft-dev.cloudfunctions.net/sitemap/assets/sitemap/products.xml');
  });


  const cachedResponse: express.RequestHandler = (req, res, next) => {
    return redisClient.get(cacheKey(req), (error: any, reply: any) => {
      reply?.length ? res.send(reply) : next();
    });
  };

  const universalRenderer: express.RequestHandler = (req, res) => {
    res.render(indexHtml, {req, providers: [{provide: APP_BASE_HREF, useValue: req.baseUrl}]}, (err: Error, html: string) => {
      if (err) res.send(err);
      if (res.statusCode === 200) redisClient.set(cacheKey(req), html, 'EX', environment.ssr.ttl_cache);
      res.send(html);
    });
  };

  const universalPreRenderer: express.RequestHandler = (req, res) => {
    req.url = req.url.replace('/ssr/', '/');
    req.originalUrl = req.originalUrl.replace('/ssr/', '/');
    res.render(indexHtml, {req, providers: [{provide: APP_BASE_HREF + 'ssr/', useValue: req.baseUrl + 'ssr/'}]}, ((err: Error, html) => {
      if (err) res.send(err);
      if (res.statusCode === 200) redisClient.set(cacheKey(req), html, 'EX', environment.ssr.ttl_cache);
      res.redirect(req.url);
    }));
  };

  server.use(morgan(':method :url :status     :remote-addr :referrer     :response-time ms :total-time ms'));
  server.get('/ssr/*', universalPreRenderer);
  server.get('*', cachedResponse, universalRenderer);
  return server;
}

function run(): void {
  const port = process.env.PORT || 4000;

  const server = app();
  server.listen(port, () => {
    console.log(`FIÜK SSR Express server listening on http://localhost:${port}`);
  });
}

// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.
declare const __non_webpack_require__: NodeRequire;
const mainModule = __non_webpack_require__.main;
const moduleFilename = mainModule && mainModule.filename || '';
if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
  run();
}

export * from './src/main.server';

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
google-oss-botcommented, Jan 19, 2022

This issue does not seem to follow the issue template. Make sure you provide all the required information.

0reactions
firstlinestudiocommented, Jan 29, 2022

@firstlinestudio thank you for your replay!

I’am downgrade my angular version to v12 and added import '@angular/fire/firestore-protos';

./server.ts:14:0-40 - Error: Module not found: Error: Can't resolve '@angular/fire/firestore-protos' in 'D:\Frontend\königs-art'

I would recommend upgrading to Firebase 9. If you must keep Firebase 8, try using the compat versions of all the Angular Fire tools. I.E. @angular/fire/compat/firestore

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Cannot find module '@firebase/app' for Angular + SSR ...
The problem is that the deployment pipeline generates a new package.json file inside the distribution folder, but it doesn't contain @firebase/ ...
Read more >
Cannot find module 'firebase/app' while deploying Angular ...
You're getting this error because you're excluding firebase dependencies with this => const regex = /firebase\/(app|firestore)/; ...
Read more >
Error running angular-fire with universal SSR - Google Groups
Error : Cannot find module 'firebase/app'. Require stack: - /home/de-10/Desktop/dist/server.js. at Function.Module.
Read more >
angular/angularfire2 - Gitter
Error : Cannot find module 'firebase/app' at Function. ... I using angular, and want the build to fail, if there is something wrong...
Read more >
Firebase JavaScript SDK Release Notes - Google
Updated "missing index" error message to include the link to create the composite ... Fixed a bug where @firebase/app-check-types was not included as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found