Your Angular code won't run on a server out-of-the-box. The module format is incorrect and your code may be coupled to the browser. To get your code running on a server you need a universal bundle.

A universal bundle is a build of your site converted for a commonjs environment like nodejs. The import statements turn into require() functions and any browser dependent code will break your server-side rendering.

The browser's DOM is not available on the server. Angular uses a browser independent implementation of the DOM: domino. This indepent solution gives you basic DOM support, but it can't do everything the browser does. You'll have to cut any code that directly manipulates the DOM or uses browser API's unsupported by domino. I'll cover this in a later blog post.

The good news is the Angular CLI makes this easy. The bad? This is where the tedious config starts.

This article is based off 4.4.3 of Angular Universal, 4.4.3 Angular, and 1.4.2 of the Angular CLI.

Install the Angular CLI

Avoid version pain. Make sure you at least have version 1.3.0 of the CLI installed. This article uses version 1.4.2 .

npm i -g @angular/cli

Set up a minimal project

Use the CLI to create a new project. The CLI provides a command that creates an Angular project with a minimal setup.

ng new ssr-project --minimal

Install platform server

Angular Universal works through the @angular/platform-server module. It doesn't come with the Angular CLI setup. You need to install it separately.

npm i @angular/platform-server --save

Make sure you've installed the same version as the other Angular modules. The CLI may install an older version of Angular such as 2.4.2 and the @angular/platform-server install may install a newer version. A mismatch of Angular module versions is going to give you a bad time later on. Go to the package.json and make sure every @angular module is on the same version. This article uses Angular 4.4.3 .

/package.json { "name": "ssr-project", "version": "0.0.0", "license": "MIT", "private": true, "dependencies": { "@angular/animations": "^4.4.3", "@angular/common": "^4.4.3", "@angular/compiler": "^4.4.3", "@angular/core": "^4.4.3", "@angular/forms": "^4.4.3", "@angular/http": "^4.4.3", "@angular/platform-browser": "^4.4.3", "@angular/platform-browser-dynamic": "^4.4.3", "@angular/platform-server": "^4.4.3", "@angular/router": "^4.4.3", "core-js": "^2.4.1", "rxjs": "^5.4.2", "zone.js": "^0.8.14" }, "devDependencies": { "@angular/cli": "1.4.3", "@angular/compiler-cli": "^4.4.3", "@angular/language-service": "^4.4.3", "typescript": "~2.3.3" } }

Make the browser module aware of a server transition

Open src/app/app.module.ts . Angular needs to know that this is a server-side rendered app. Use the BrowserModule.withServerTransition() method and provide a unique id. This method acts a generic interface between the client and the server. This allows Angular to do any specific processing to take over a server-side rendered site.

/src/app/app.server.module.ts import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule.withServerTransition({ appId: 'ssr-app' }) ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }

Create an AppServerModule

The browser needs an AppModule and the server needs one as well. This is a place where you can override or cancel-out any browser specific code for server compatibility.

Create the file src/app/app.server.module.ts .

/src/app/app.server.module.ts import { NgModule } from '@angular/core'; import { ServerModule } from '@angular/platform-server'; import { AppModule } from './app.module'; import { AppComponent } from './app.component'; @NgModule({ imports: [ AppModule, ServerModule ], bootstrap: [ AppComponent ] }) export class AppServerModule { }

Create an entry point for the server module

Each top-level Angular module needs an entry point file to start the application. Create a main.server.ts file an export the AppServerModule just like in main.ts .

src/main.server.ts export { AppServerModule } from './app/app.server.module';

Set up the server module's TypeScript configuration

At the point you've created the ServerModule and it's entry point. However, there's no TypeScript configuration to build it to JavaScript. You might be thinking, "Oh, I'll just add it to the "files" array in the tsconfig.app.json ." There's a big problem with that. The current configuration is uses the es2015 module setting and you need commonjs . The Angular compiler also needs to customize the configuration to know what the entry module is for Angular Universal. The easiest way to fix this by creating a specific server configuration: src/tsconfig.server.json .

/src/tsconfig.server.json { "extends": "../tsconfig.json", "compilerOptions": { "outDir": "../out-tsc/app", "baseUrl": "./", "module": "commonjs", "types": [] }, "exclude": [ "test.ts", "**/*.spec.ts" ], "angularCompilerOptions": { "entryModule": "app/app.server.module#AppServerModule" } }

Tell the CLI how to build the universal bundle

The CLI is a big abstraction on Webpack. It knows how to build Angular, so it sets up the configuration and you command it. You may need to customize your configuration in certain cases. Creating a universal bundle is one of those cases.

Open the .angular-cli.json file. This file contains the default configuration options. To create a universal bundle you need to create a separater "app" entry. Add the following entry to the "apps" array.

/.angular-cli.json { "platform": "server", "root": "src", "outDir": "functions", "assets": [ "assets", "favicon.ico" ], "index": "index.html", "main": "main.server.ts", "test": "test.ts", "tsconfig": "tsconfig.server.json", "testTsconfig": "tsconfig.spec.json", "prefix": "app", "styles": [ "styles.css" ], "scripts": [], "environmentSource": "environments/environment.ts", "environments": { "dev": "environments/environment.ts", "prod": "environments/environment.prod.ts" } }

This configuration is the bow on all the hard work you've done so far. It tells the CLI that this "app" runs on a server "platform" , has an entry file of main.server.ts and is built with the tsconfig.server.json configuration file.

Now build it

Use the CLI to specify that you want to build the server app entry.

ng build --app 1 --prod --output-hashing none

This should give you an output that looks something like this:

Date: 2017-09-26T12:39:17.053Z Hash: 53b3312d5cbe854aac7c Time: 4791ms chunk {0} main.bundle.js (main) 7.13 kB [entry] [rendered] chunk {1} styles.bundle.css (styles) 0 bytes [entry] [rendered]

Success! You have a universal bundle. What about those flags? The --prod flag minified and enabled AoT. The --output-hashing none flag removed any hashes from file names. These hashes are good for browser caching, but this makes them useless and frustrating on the server.

Speaking of servers, it's time that you set one up.