Skip to content

Error with angular 9 SSR and fire base cloud functions #2369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
khaled6mmd opened this issue Mar 29, 2020 · 18 comments
Closed

Error with angular 9 SSR and fire base cloud functions #2369

khaled6mmd opened this issue Mar 29, 2020 · 18 comments

Comments

@khaled6mmd
Copy link

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

@jamesdaniels
Copy link
Member

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.

@khaled6mmd
Copy link
Author

khaled6mmd commented Mar 29, 2020

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?

@jamesdaniels
Copy link
Member

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

@khaled6mmd
Copy link
Author

khaled6mmd commented Mar 30, 2020

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

@jamesdaniels
Copy link
Member

@khaled6mmd
Copy link
Author

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
Copy link
Author

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

@hiepxanh
Copy link
Contributor

hiepxanh commented Apr 1, 2020

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

@khaled6mmd
Copy link
Author

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> 

@hiepxanh
Copy link
Contributor

hiepxanh commented Apr 2, 2020

image
image

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

@khaled6mmd
Copy link
Author

image
image

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

I use nvm to do so, did not help

@jamesdaniels
Copy link
Member

@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.

@khaled6mmd
Copy link
Author

@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

@Silvac34
Copy link

Silvac34 commented Apr 3, 2020

@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); });

@khaled6mmd
Copy link
Author

@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/fire@6.0.0-rc.2 --save

then

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

@Silvac34
Copy link

Silvac34 commented Apr 3, 2020

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

@khaled6mmd
Copy link
Author

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

@Teebo
Copy link

Teebo commented May 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants