Search code examples
angularangular17

How to change the port of ng build with Angular 17


I have tried to use the deploy url with localhost:4100 but this did not change the port of the final builded server.mjs file in the dist folder I would like to to have a different port than 4000 when i run the server.mjs file on a remote virtual machine


Solution

  • If you are talking about the listening port of the server side part of an Angular application that uses server side rendering, then this is not a build configuration, you have to configure it inside your server.ts file.

    Inside your server.ts you should find a run function, you can either modify the port here, or if you havn't modified it, you could add an .env file with a PORT variable inside of it.

    server.ts
    function run(): void {
      // Here the listening port is defined.
      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}`);
      });
    }