angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Error with angular 9 SSR and fire base cloud functions

Open khaled6mmd opened this issue 6 years ago • 18 comments

I have been trying for two days now, I'm front end developer so I don't have the require experience to use node and the cloud function

I don't know if the following information is sufficient

so if you need any more information ask please

this is the log of fire base

Error: Cannot find module '/srv/dist/coupons/server'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at exports.universal.functions.https.onRequest (/srv/lib/index.js:5:5)
    at cloudFunction (/srv/node_modules/firebase-functions/lib/providers/https.js:49:9)
    at /worker/worker.js:783:7
    at /worker/worker.js:766:11
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)

and this is my index.ts

import * as functions from 'firebase-functions';

export const universal = functions.https.onRequest((request, response) => {
  require(`${process.cwd()}/dist/coupons/server`).app(request, response);
});

and this is sever.ts

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

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

import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';

// The Express app is exported so that it can be used by serverless Functions.
export function app() {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/coupons/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html'))
    ? 'index.original.html'
    : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine(
    'html',
    ngExpressEngine({
      bootstrap: AppServerModule
    })
  );

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

  // Example Express Rest API endpoints
  // app.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get(
    '*.*',
    express.static(distFolder, {
      maxAge: '1y'
    })
  );

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}

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

  // Start up the Node server
  const server = app();
  server.listen(port, () => {
    console.log(`Node 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';

i followed this guide to convert the app to SSR

Deploying your Universal application on Cloud Functions for Firebase

any help would be appreciated

khaled6mmd avatar Mar 29 '20 20:03 khaled6mmd

Universal in Angular 9 has had a lot changed & most of our instructions are outdated. The next RC for 6 will see the release of a schematic for universal support on Cloud Functions. Standby.

jamesdaniels avatar Mar 29 '20 21:03 jamesdaniels

Universal in Angular 9 has had a lot changed & most of our instructions are outdated. The next RC for 6 will see the release of a schematic for universal support on Cloud Functions. Standby.

Any information about how much it's going to take to be released?

khaled6mmd avatar Mar 29 '20 22:03 khaled6mmd

The next RC was released today, this includes support for schematic deploys of Universal apps to Hosting & Cloud Functions.

jamesdaniels avatar Mar 30 '20 23:03 jamesdaniels

Could you provide link please, I could not find any updated docs for deployment

khaled6mmd avatar Mar 30 '20 23:03 khaled6mmd

https://github.com/angular/angularfire/blob/master/docs/deploy/getting-started.md

jamesdaniels avatar Mar 31 '20 01:03 jamesdaniels

https://github.com/angular/angularfire/blob/master/docs/deploy/getting-started.md

thank you very very much

one last thing, what does it mean

If you've specified that you want a server-side rendering enabled deployment in a firebase function, the command will also

because this is exactly what I need

and also,

ng deploy --preview

gives

Unknown option: '--preview'

thanks in advance, sorry for asking to much

khaled6mmd avatar Mar 31 '20 03:03 khaled6mmd

is there a chance you would update the following docs? https://github.com/angular/angularfire/tree/master/docs/universal

khaled6mmd avatar Mar 31 '20 09:03 khaled6mmd

I'm successful runing angular universal with angular 9.1 and @angular/fire 6RC2 at here https://github.com/hiepxanh/angular-fire-universal @khaled6mmd I just create an PR here https://github.com/angular/angularfire/pull/2372/files?short_path=e76f33d#diff-e76f33db0439ee8e320dfa7ecf47f09b let's check this out @jamesdaniels can you review my PR? https://github.com/angular/angularfire/pull/2372

hiepxanh avatar Apr 01 '20 06:04 hiepxanh

I'm successful runing angular universal with angular 9.1 and @angular/fire 6RC2 at here https://github.com/hiepxanh/angular-fire-universal @khaled6mmd I just create an PR here https://github.com/angular/angularfire/pull/2372/files?short_path=e76f33d#diff-e76f33db0439ee8e320dfa7ecf47f09b let's check this out @jamesdaniels can you review my PR? #2372

thank you very much for the efforts

I follow you instructions and it gave me the following

Built at: 04/01/2020 4:18:16 PM
  Asset      Size  Chunks                    Chunk Names
main.js  3.93 MiB       0  [emitted]  [big]  main
Entrypoint main [big] = main.js
chunk    {0} main.js (main) 7.37 MiB [entry] [rendered]
⚠️ Your Node.js version (13.12.0) does not match the Firebase Functions runtime (10).
Deploying your Angular Universal application...
success.hosting.split is not a function
PS C:\Users\Khaled\Desktop\coupons> 

khaled6mmd avatar Apr 01 '20 14:04 khaled6mmd

image image

@khaled6mmd please follow this instruction https://firebase.google.com/docs/functions/manage-functions

hiepxanh avatar Apr 02 '20 01:04 hiepxanh

image image

@khaled6mmd please follow this instruction https://firebase.google.com/docs/functions/manage-functions

I use nvm to do so, did not help

khaled6mmd avatar Apr 02 '20 09:04 khaled6mmd

@khaled6mmd the error you posted seems to indicate that you're using an unsupported version of Node, maybe your nvm setup is not working correctly.

jamesdaniels avatar Apr 02 '20 15:04 jamesdaniels

@khaled6mmd the error you posted seems to indicate that you're using an unsupported version of Node, maybe your nvm setup is not working correctly.

@jamesdaniels installed node 10.13 as it's the minimum to run ng cli and it gives same error

khaled6mmd avatar Apr 02 '20 20:04 khaled6mmd

@hiepxanh , could you add to your repo your functions folder to have an example off ssr functions ? I can't manage to make it work with :

export const universal = functions.https.onRequest((request, response) => { require(${process.cwd()}/dist/server/main).app(request, response); });

Silvac34 avatar Apr 03 '20 14:04 Silvac34

@hiepxanh , could you add to your repo your functions folder to have an example off ssr functions ? I can't manage to make it work with :

export const universal = functions.https.onRequest((request, response) => { require(${process.cwd()}/dist/server/main).app(request, response); });

it gets created automatically if you follow this guide

https://github.com/angular/angularfire/blob/master/docs/deploy/getting-started.md

then run

npm i @angular/[email protected] --save

then

https://github.com/angular/angularfire/pull/2372/files#diff-e76f33db0439ee8e320dfa7ecf47f09b

khaled6mmd avatar Apr 03 '20 15:04 khaled6mmd

Getting " Command failed: npm ls || true " as raised here

Silvac34 avatar Apr 03 '20 15:04 Silvac34

Getting " Command failed: npm ls || true " as raised here

but it does work fine with me,

ng deploy --project=**** and ng deploy --preview

work fine for me, I get another error so I think I need to close this issue and create another one for the issue I have now

khaled6mmd avatar Apr 03 '20 15:04 khaled6mmd

Hi @khaled6mmd what was the solution to this issue? I have the same

Teebo avatar May 27 '21 14:05 Teebo