universal: ECONNREFUSED 127.0.0.1:53233 when running "serve-ssr" from @nguniversal/builders
We are using Angular Universal in production and are very happy with it. We want to work with server side rendering all the way down to our day-to-day development lifecycle and have chosen to use the @nguniversal/builders
package to do that.
Unfortunately, since upgrading to Angular 10.x and @nguniversal/builders 10.x, the schematic serve-ssr
from @nguniversal/builders
fails with the following error:
connect ECONNREFUSED 127.0.0.1:55304
đ Bug report
What modules are related to this issue?
- aspnetcore-engine
- builders
- common
- express-engine
- hapi-engine
Is this a regression?
Yes, this did not happen with Angular 9.x and @nguniversal/builders 9.x but it now happens with version 10.x.
Description
After a lot of debugging, Iâve found a curious behavior: It seems as though the node net server created by @nguniversal/builders
succesfully starts to listen, but after a few seconds shuts down silently. Iâve recorded this behavior and it can be seen in this short video.
Context:
- Right-side terminal is watching all ports being listened to locally
- Left side terminal is the
server-ssr
command being executed - At the beginning of the build, a port is opened by node (55304)
- After a few seconds, node stops listening on 55304 silently
- Once the build (browser and server) succeeds, my SSR server successfully starts on 4200
- The âbuilder processâ now tries to connect to 55304 but itâs no longer there, thus failing the command with
connect ECONNREFUSED 127.0.0.1:55304
Curiously, the command works when setting "inspect": true
in the config of @nguniversal/builders (angular.json). This forces us to connect a debugger to the process though, and isnât very developer friendly.
đŹ Minimal Reproduction
Unfortunately, I donât have a reproduction environment.
đ„ Exception or Error
connect ECONNREFUSED 127.0.0.1:55304
đ Your Environment
Angular CLI: 10.1.2
Node: 12.18.2
OS: darwin x64
Angular: 10.1.2
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router
Ivy Workspace: Yes
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1001.2
@angular-devkit/build-angular 0.1001.2
@angular-devkit/core 10.1.2
@angular-devkit/schematics 10.1.2
@nguniversal/builders 10.1.0
@nguniversal/express-engine 10.1.0
@schematics/angular 10.1.2
@schematics/update 0.1001.2
rxjs 6.6.3
typescript 4.0.2
This is maybe related to https://github.com/angular/universal/issues/1796
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 19
- Comments: 28
Hey, youâre not alone. I can confirm that when upgrading
Angular
8
to10
I could face exactly same issue once trying to run with the newserver-ssr
schematic.This is indeed a very annoying bug: It makes local development with SSR almost impossible since one has to rebuild the full prod bundle on each change.
Have the same issue. I tried to investigate the source code of the
@nguniversal/builders
package and I discovered that if I increase the number of attempts here: https://github.com/angular/universal/blob/3ca407beb3a4f5d96f140cf47c259f80b62f3ae5/modules/builders/src/ssr-dev-server/utils.ts#L79 then it works well.It seems like my app doesnât boot fast enought to be connected to the dev-ssr process before the number of attempts are exceeded.
Thanks for reporting this issue. However, you didnât provide sufficient information for us to understand and reproduce the problem. Please check out our submission guidelines to understand why we canât act on issues that are lacking important information.
If the problem persists, please file a new issue and ensure you provide all of the required information when filling out the issue template.
Iâam seeing this error with the dev:ssr NPM script.
I tried to change attemts to 100 in https://github.com/angular/universal/blob/3ca407beb3a4f5d96f140cf47c259f80b62f3ae5/modules/builders/src/ssr-dev-server/utils.ts#L79
The server lasted much longer, but still died in the end.
I believe the above code waits for the server to start on a randomized port. Hence the ECONNREFUSED 127.0.0.1:53233
This solved the dev-server crashing for me:
I believe we need to check process.env.PORT, and start express on the randomized port for the waiting logic to work. Then there probably is some proxy-magic that forwards the random port to 4200?
Edit: ^ This is the same problem as @rsouthgate describes
I understand why this was closed and that resources are limited, but for cases like this that are experienced by a significant number of users who arenât able to identify the source of the issue, can the @nguniversal team consider handling these more like customer service requests than backlog items, and help share the repro burden? Maybe have an intern look at it? Even if itâs not bug-related, itâs clearly still a common âgotchaâ; thereâs value in finding and documenting the cause and any workarounds.
I get this same error trying to enable SSR with a .NET 6/Angular 14 app, but only when
architect:server:options:main
is set tosrc\main.server.ts
. When I set it toserver.ts
, I get a âthe module at âŠ\main.js does not export a default functionâ error via a NodeInvocationException.The Angular Universal downloadable sample shows it set to
server.ts
, but this super-helpful article points it tosrc\main.server.ts
. Which should it be?@alan-agius4 & Angular team, with Microsoft obsoleting their
SpaServices
andNodeServices
three years ago now, can the community implore you guys again to reach out to Microsoft and create a doc/repo of unified guidance for developing and deploying .NET/Angular projects with Angular Universal SSR? Weâre working with sticks and rocks from kind-hearted community members and a dozen dead-end StackOverflow threads.I tried running yarn serve:ssr instead which also also failed but it printed some useful errors in my console.
In my case it was caused by trying to access the window. I added a couple of if statements and now it works.
I just tried installing angular universal in my angular 13 app. When I run yarn dev:ssr, after it says âCompiled succesfullyâ it seems like it prints out the entire dist/server/main.js in red, and at the bottom it just says:
How am I supposed to debug this?
@MarkPieszak Thanks for the suggestion. Did you actually edit the file in the
node_modules
folder ? That doesnât sound like a viable solution đHad the similar problem as @rsouthgate. Changing the port to 443 (with ssl keys provided) depending on the environment (for local one). Wasnât able to debug whatâs happening.
Stumbled across this and discovered for me it was that iâd changed the
process.env.PORT
in server.tsrun()
toprocess.env.WEBSITES_PORT
(Azure App Services passes that env var through by default). So it would seem that the dev server passes the port to the child process via the hard coded PORT env var invisibly.