0

I'm currently trying to deploy my Angular Universal App to Firebase Functions & Hosting. Therefore I'm following this tutorial.

Error: Error parsing triggers: Cannot find module 'require("./server/main")'

Require stack:
- /Users/timfuhrmann/Documents/Entwicklung/norebro/functions/dist/server.js
- /Users/timfuhrmann/Documents/Entwicklung/norebro/functions/lib/index.js
- /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js

I figured that it has something to do with webpack.server.config.js. As soon as I add library & libraryTarget inside of output, the error appears for firebase deploy and npm run serve:ssr:

...
output: {
    // Puts the output at the root of the dist folder
    path: path.join(__dirname, 'dist'),
    library: 'app',
    libraryTarget: 'umd',
    filename: '[name].js'
},
...


Complete log:

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'serve:ssr' ]
2 info using npm@6.12.0
3 info using node@v12.6.0
4 verbose run-script [ 'preserve:ssr', 'serve:ssr', 'postserve:ssr' ]
5 info lifecycle norebro@0.0.0~preserve:ssr: norebro@0.0.0
6 info lifecycle norebro@0.0.0~serve:ssr: norebro@0.0.0
7 verbose lifecycle norebro@0.0.0~serve:ssr: unsafe-perm in lifecycle true
8 verbose lifecycle norebro@0.0.0~serve:ssr: PATH: /usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/timfuhrmann/Documents/Entwicklung/norebro/node_modules/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Library/Apple/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/timfuhrmann/Documents/Entwicklung/norebro/node_modules/.bin
9 verbose lifecycle norebro@0.0.0~serve:ssr: CWD: /Users/timfuhrmann/Documents/Entwicklung/norebro
10 silly lifecycle norebro@0.0.0~serve:ssr: Args: [ '-c', 'node dist/server' ]
11 silly lifecycle norebro@0.0.0~serve:ssr: Returned: code: 1  signal: null
12 info lifecycle norebro@0.0.0~serve:ssr: Failed to exec serve:ssr script
13 verbose stack Error: norebro@0.0.0 serve:ssr: `node dist/server`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:203:13)
13 verbose stack     at ChildProcess.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:203:13)
13 verbose stack     at maybeClose (internal/child_process.js:1021:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)
14 verbose pkgid norebro@0.0.0
15 verbose cwd /Users/timfuhrmann/Documents/Entwicklung/norebro
16 verbose Darwin 19.0.0
17 verbose argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "serve:ssr"
18 verbose node v12.6.0
19 verbose npm  v6.12.0
20 error code ELIFECYCLE
21 error errno 1
22 error norebro@0.0.0 serve:ssr: `node dist/server`
22 error Exit status 1
23 error Failed at the norebro@0.0.0 serve:ssr script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]


EDIT:
At first when you open the website, there won't be HTML instead of app-root. As soon as you change the route though and navigate to a different component, there is HTML instead of app-root - how come?

Firebase Functions console sometimes returns a successful execution, sometimes some @type information and sometimes even errors:

TypeError: handler is not a function
    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 ZoneDelegate.invokeTask (/srv/dist/server.js:575:31)
    at Zone.runTask (/srv/dist/server.js:347:47)
    at ZoneTask.invokeTask (/srv/dist/server.js:650:34)
    at ZoneTask.invoke (/srv/dist/server.js:639:48)
    at data.args.(anonymous function) (/srv/dist/server.js:1619:25)
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Tom
  • 3,672
  • 6
  • 20
  • 52
  • I am sorry my answer didn't work. I managed to make it work without using `library` and `libraryTarget` properties (so just remove them). I updated the answer to reflect on this. Can you also try removing these properties and see if it works? Tomorrow, I will try and investigate why these properties were added in the original tutorial, and whether or not was removing them was the right choice... – miselking Oct 16 '19 at 23:09
  • Thank you for your efforts! Removing them actually works, but another error appears instead - see edit! – Tom Oct 16 '19 at 23:39
  • 1
    I updated the solution, with this new approach `app-root` is replaced with HTML (if it isn't then SSR doesn't work) and no error should appear. Hopefully there will be no errors, but if there are you can reply in comments here, I'll try and solve it... :) **P.S**: New approach only has additional changes in `webpack.server.config.js`. – miselking Oct 17 '19 at 13:17
  • @miselking Thank you! By changing external, I was able to add both properties without any errors and could successfully deploy. I don't know why, but at first when you open the website, there won't be HTML instead of `app-root`. As soon as you change the route though and navigate to a different component, there is HTML instead of `app-root` - how come? – Tom Oct 17 '19 at 15:52
  • @miselking In other words, solely the initial ' ' Link/Route won't display anything, for some routes there's HTML instead of `app-root` and for one route a timeout in both the browser and firebase console occurs when I'm trying to inspect the source code. – Tom Oct 17 '19 at 16:13
  • Ok I've been able to solve the timeout issue, but smh the HTML for the initial route wont appear. – Tom Oct 17 '19 at 16:33
  • It is because Firebase keeps serving the `index.html` file from your `dist/browser` folder (the static one with only `` part) instead of running `ssr` function. If you remove it then it should work. Check the updated answer - part about working with `copy-angular-app.js`. – miselking Oct 17 '19 at 23:00
  • @miselking Thank you so much! I assume it works, since `app-root` is being replaced with HTML. I let you know, in case of any further error ;) Thanks again! – Tom Oct 18 '19 at 00:04
  • @miselking Because sometimes I get weird errors inside of the function's console - e.g. `Cannot match any routes. URL Segment: 'apple-touch-icon-120x120-precomposed.png'` - but I think that's another issue I have to figure out. – Tom Oct 18 '19 at 00:06

1 Answers1

0

For TypeError: handler is not a function error, simply replace const app = express(); with export const app = express(); in your server.ts file

source: https://stackoverflow.com/a/58447459/12595124