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

Most upvoted comments

Hey, you’re not alone. I can confirm that when upgrading Angular 8 to 10 I could face exactly same issue once trying to run with the new server-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:

const port = process.env.PORT || 4000 // <--- process.env.PORT holds the randomized port. Use 4000 for production builds.
expressApp.listen(port, () => {
  console.log(`Node Express server listening on http://localhost:${port}`);
})

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 to src\main.server.ts. When I set it to server.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 to src\main.server.ts. Which should it be?

@alan-agius4 & Angular team, with Microsoft obsoleting their SpaServices and NodeServices 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:

A server error has occurred.
node exited with 1 code.
connect ECONNREFUSED 127.0.0.1:57222

How am I supposed to debug this?

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.

@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.ts run() to process.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.