Skip to content

Angular SSR with NestJS #31443

@alexcibotari

Description

@alexcibotari

Which @angular/* package(s) are relevant/related to the feature request?

platform-server

Description

At the moment, the default SSR solution is ExpressJS., Please add the possibility to also use NestJS, as it improves the overall development experience. At the moment solution looks like this, but the problem is when creating a production bundle, where we receive errors.

import {createNodeRequestHandler, isMainModule,} from '@angular/ssr/node';
import {dirname, resolve} from 'node:path';
import {fileURLToPath} from 'node:url';
import {NestFactory} from '@nestjs/core';
import {AppModule} from './server/app.module';
import {ExpressAdapter, NestExpressApplication} from '@nestjs/platform-express';
import express from 'express';

const serverDistFolder = dirname(fileURLToPath(import.meta.url));
const browserDistFolder = resolve(serverDistFolder, '../browser');

const expressApp = express();

async function bootstrap() {
  const adapter = new ExpressAdapter(expressApp);
  const nestApp = await NestFactory.create<NestExpressApplication>(AppModule, adapter);
  await nestApp.init();
}


await bootstrap();

/**
 * Start the server if this module is the main entry point.
 * The server listens on the port defined by the `PORT` environment variable, or defaults to 4000.
 */
//if (import.meta.url.endsWith('/server.mjs')) {
if (isMainModule(import.meta.url)) {
  const port = process.env['PORT'] || 4000;
  expressApp.listen(port, () => {
    console.log(`NestJS server listening on http://localhost:${port}`);
  });
}

/**
 * Request handler used by the Angular CLI (for dev-server and during build) or Firebase Cloud Functions.
 */
export const reqHandler = createNodeRequestHandler(expressApp);

Error

Prerendered 0 static routes.
Application bundle generation failed. [8.136 seconds] - 2025-10-12T19:16:48.474Z

X [ERROR] Could not resolve "@nestjs/websockets/socket-module"

    node_modules/@nestjs/core/nest-application.js:24:94:
      24 │ ...ocket-module', () => require('@nestjs/websockets/socket-module'));
         ╵                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                                

  You can mark the path "@nestjs/websockets/socket-module" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.


X [ERROR] Could not resolve "@nestjs/microservices/microservices-module"

    node_modules/@nestjs/core/nest-application.js:27:104:
      27 │ ...le', () => require('@nestjs/microservices/microservices-module'));
         ╵                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                                

  You can mark the path "@nestjs/microservices/microservices-module" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.


X [ERROR] Could not resolve "@nestjs/microservices"

    node_modules/@nestjs/core/nest-application.js:135:99:
      135 │ ...ervices', 'NestFactory', () => require('@nestjs/microservices'));
          ╵                                           ~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                                

  You can mark the path "@nestjs/microservices" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.


X [ERROR] Could not resolve "@nestjs/microservices"

    node_modules/@nestjs/core/nest-factory.js:60:99:
      60 │ ...services', 'NestFactory', () => require('@nestjs/microservices'));
         ╵                                            ~~~~~~~~~~~~~~~~~~~~~~~                                                                                                                                

  You can mark the path "@nestjs/microservices" as external to exclude it from the bundle, which will remove this error and leave the unresolved path in the bundle. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.

Proposed solution

Provide a solution that will support NestJS

Alternatives considered

Improve Bundler to not fail during build

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions