2

I recently added SSR to my Angular app with Angular Universal (with express as the server) and I'm having trouble updaing my build and deploy steps. I was using a dockerfile in which I was having a build step: ng build -c <environtmentName> followed by a startup command CMD ["node", "./dist/MyApp/main.js"].

After adding SSR the new structure came with a couple of new commands in package.json, including one for "build": "build:ssr": "ng build --prod && ng run MyApp:server:production".

I am now trying to add a "pure" build script that would build both the frontend app plus the server related content, like server.ts. I have noticed that if I run the ng build command only the angular app is being built. If I however run the build:ssr command though it builds the app in the first part (ng build --prod) and then it compiles and runs the server in the second ng run MyApp:server:production

I was not able to find a command that builds the server separately so that I can include it in my dockerfile. I do not want to run ng run MyApp:server:production in my dockerfile, I simply want to run it as the CMD ["node", "./dist/MyApp/server/main.js"] command for various reasons. The only workaround was to run the build:ssr - or equivalent commands related to different environments I'm targeting - locally, then build the docker image, ng build simply ignores the express server related files it seems.

Vee6
  • 1,527
  • 3
  • 21
  • 40
  • Normally you need to build your server separately – AndyNope Apr 19 '22 at 12:38
  • @AndyNope the operation takes place during `ng run MyApp:server:production`. Is there any way I can isolate that part or is it just "magical" and I have to write my own script manually? – Vee6 Apr 19 '22 at 12:51
  • To be honest; I never deployed the server part myself and let GitLab do that automatically for me, but I can check the config at my projects... just a moment – AndyNope Apr 19 '22 at 12:53
  • https://angular.io/guide/universal Like I thought, you have to do a normal Build ng build --prod and then start the SSR separately... But I don't want to confuse the you because I've never done it manually. I am also following this now as I am interested in the correct solution. – AndyNope Apr 19 '22 at 13:02
  • @AndyNope yes, that's what the guide says and the server build is part of running the SSR, however for my docker setup I need to be running the server build separately from the step where I start the server. Thanks though – Vee6 Apr 19 '22 at 13:13
  • `npm run build:ssr` – Pieterjan Apr 27 '22 at 12:25
  • @Pieterjan that builds AND starts the server. I need it to just build, either server + frontend or just the "server" express part – Vee6 Apr 27 '22 at 14:40
  • `npm run start:ssr` starts the server. `npm run build:ssr` only generates the browser+server bundles – Pieterjan Apr 27 '22 at 22:20
  • @Pieterjan when adding angular universal only `build:ssr` was added as as npm task, and the contents are these: `"build:ssr": "ng build --prod && ng run MyApp:server:production",`, there is no `start:ssr` for me. And `ng build` only does the client stuff, as mentioned, the server code is not compiled. Can you show me what you meant by `build:ssr` doing only the (full) build? Or could you show me how it looks in one of your projects? – Vee6 May 03 '22 at 13:04
  • [package.json](https://github.com/MintPlayer/MintPlayer/blob/master/MintPlayer.Web/ClientApp/package.json#L17) – Pieterjan May 03 '22 at 14:13

0 Answers0