title: StaticInjectorError Right-hand side of 'instanceof' is not an object issue when doing ssr in angular 6
body: <p>I am trying to do ssr in angular 6 with the help of <a href="https://github.com/angular/angular-cli/wiki/stories-universal-rendering" rel="noreferrer">this</a> link,but it shows following error</p>
  
  <pre><code>Node server listening on http://localhost:4000
  TypeError: StaticInjectorError[InjectionToken Application Initializer -&gt; InjectionToken DocumentToken]:
    StaticInjectorError(Platform: core)[InjectionToken Application Initializer -&gt; InjectionToken DocumentToken]:
      Right-hand side of 'instanceof' is not an object
  </code></pre>
  
  <p><strong>server.ts</strong></p>
  
  <pre><code>// These are important and needed before anything else
  import 'zone.js/dist/zone-node';
  import 'reflect-metadata';
  
  import { renderModuleFactory } from '@angular/platform-server';
  import { enableProdMode } from '@angular/core';
  
  import * as express from 'express';
  import { join } from 'path';
  import { readFileSync } from 'fs';
  
  // Faster server renders w/ Prod mode (dev mode never needed)
  enableProdMode();
  
  // Express server
  const app = express();
  
  const PORT = process.env.PORT || 4000;
  const DIST_FOLDER = join(process.cwd(), 'dist');
  
  // Our index.html we'll use as our template
  const template = readFileSync(join(DIST_FOLDER, 'angular6ssr', 'index.html')).toString();
  
  // * NOTE :: leave this as require() since this file is built Dynamically from webpack
  const { AppServerModuleNgFactory, LAZY_MODULE_MAP } = require('./dist/server/main');
  
  const { provideModuleMap } = require('@nguniversal/module-map-ngfactory-loader');
  
  app.engine('html', (_, options, callback) =&gt; {
    renderModuleFactory(AppServerModuleNgFactory, {
      // Our index.html
      document: template,
      url: options.req.url,
      // DI so that we can get lazy-loading to work differently (since we need it to just instantly render it)
      extraProviders: [
        provideModuleMap(LAZY_MODULE_MAP)
      ]
    }).then(html =&gt; {
      callback(null, html);
    });
  });
  
  app.set('view engine', 'html');
  app.set('views', join(DIST_FOLDER, 'angular6ssr'));
  
  // Server static files from /browser
  app.get('*.*', express.static(join(DIST_FOLDER, 'angular6ssr')));
  
  // All regular routes use the Universal engine
  app.get('*', (req, res) =&gt; {
    res.render(join(DIST_FOLDER, 'angular6ssr', 'index.html'), { req });
  });
  
  // Start up the Node server
  app.listen(PORT, () =&gt; {
    console.log(`Node server listening on http://localhost:${PORT}`);
  });
  </code></pre>
  
  <p><strong>angular.json</strong></p>
  
  <pre><code>{
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "version": 1,
    "newProjectRoot": "projects",
    "projects": {
      "angular6ssr": {
        "root": "",
        "sourceRoot": "src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {},
        "architect": {
          "build": {
            "builder": "@angular-devkit/build-angular:browser",
            "options": {
              "outputPath": "dist/angular6ssr",
              "index": "src/index.html",
              "main": "src/main.ts",
              "polyfills": "src/polyfills.ts",
              "tsConfig": "src/tsconfig.app.json",
              "assets": [
                "src/favicon.ico",
                "src/assets"
              ],
              "styles": [
                "src/styles.css"
              ],
              "scripts": []
            },
            "configurations": {
              "production": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                  }
                ],
                "optimization": true,
                "outputHashing": "all",
                "sourceMap": false,
                "extractCss": true,
                "namedChunks": false,
                "aot": true,
                "extractLicenses": true,
                "vendorChunk": false,
                "buildOptimizer": true
              }
            }
          },
          "serve": {
            "builder": "@angular-devkit/build-angular:dev-server",
            "options": {
              "browserTarget": "angular6ssr:build"
            },
            "configurations": {
              "production": {
                "browserTarget": "angular6ssr:build:production"
              }
            }
          },
          "extract-i18n": {
            "builder": "@angular-devkit/build-angular:extract-i18n",
            "options": {
              "browserTarget": "angular6ssr:build"
            }
          },
          "test": {
            "builder": "@angular-devkit/build-angular:karma",
            "options": {
              "main": "src/test.ts",
              "polyfills": "src/polyfills.ts",
              "tsConfig": "src/tsconfig.spec.json",
              "karmaConfig": "src/karma.conf.js",
              "styles": [
                "styles.css"
              ],
              "scripts": [],
              "assets": [
                "src/favicon.ico",
                "src/assets"
              ]
            }
          },
          "lint": {
            "builder": "@angular-devkit/build-angular:tslint",
            "options": {
              "tsConfig": [
                "src/tsconfig.app.json",
                "src/tsconfig.spec.json"
              ],
              "exclude": [
                "**/node_modules/**"
              ]
            }
          },
          "server": {
      "builder": "@angular-devkit/build-angular:server",
      "options": {
        "outputPath": "dist/server",
        "main": "src/main.server.ts",
        "tsConfig": "src/tsconfig.server.json"
      },
           "configurations": {
              "production": {
                "fileReplacements": [
                  {
                    "replace": "src/environments/environment.ts",
                    "with": "src/environments/environment.prod.ts"
                  }
                ]
              }
            }
    }
        }
      },
      "angular6ssr-e2e": {
        "root": "e2e/",
        "projectType": "application",
        "architect": {
          "e2e": {
            "builder": "@angular-devkit/build-angular:protractor",
            "options": {
              "protractorConfig": "e2e/protractor.conf.js",
              "devServerTarget": "angular6ssr:serve"
            }
          },
          "lint": {
            "builder": "@angular-devkit/build-angular:tslint",
            "options": {
              "tsConfig": "e2e/tsconfig.e2e.json",
              "exclude": [
                "**/node_modules/**"
              ]
            }
          }
        }
      }
    },
    "defaultProject": "angular6ssr"
  }
  </code></pre>
  
  <p><strong>package.json</strong></p>
  
  <pre><code>{
    "name": "angular6ssr",
    "version": "0.0.0",
    "scripts": {
      "ng": "ng",
      "start": "ng serve",
      "build": "ng build",
      "test": "ng test",
      "lint": "ng lint",
      "e2e": "ng e2e",
      "build:ssr": "npm run build:client-and-server-bundles &amp;&amp; npm run webpack:server",
      "serve:ssr": "node dist/server.js",
      "build:client-and-server-bundles": "ng build --prod &amp;&amp; ng build --build-optimizer --aot --environment prod --app 1 --output-hashing=false",
      "webpack:server": "webpack --config webpack.server.config.js --progress --colors"
    },
    "private": true,
    "dependencies": {
      "@angular/animations": "^6.0.0",
      "@angular/common": "^6.0.0",
      "@angular/compiler": "^6.0.0",
      "@angular/core": "^6.0.0",
      "@angular/forms": "^6.0.0",
      "@angular/http": "^6.0.0",
      "@angular/platform-browser": "^6.0.0",
      "@angular/platform-browser-dynamic": "^6.0.0",
      "@angular/platform-server": "^6.0.5",
      "@angular/router": "^6.0.0",
      "@nguniversal/module-map-ngfactory-loader": "^6.0.0",
      "core-js": "^2.5.4",
      "rxjs": "^6.0.0",
      "ts-loader": "^4.4.1",
      "zone.js": "^0.8.26"
    },
    "devDependencies": {
      "@angular-devkit/build-angular": "~0.6.0",
      "@angular/cli": "~6.0.0",
      "@angular/compiler-cli": "^6.0.0",
      "@angular/language-service": "^6.0.0",
      "@types/jasmine": "~2.8.6",
      "@types/jasminewd2": "~2.0.3",
      "@types/node": "~8.9.4",
      "codelyzer": "~4.2.1",
      "jasmine-core": "~2.99.1",
      "jasmine-spec-reporter": "~4.2.1",
      "karma": "~1.7.1",
      "karma-chrome-launcher": "~2.2.0",
      "karma-coverage-istanbul-reporter": "~1.4.2",
      "karma-jasmine": "~1.1.1",
      "karma-jasmine-html-reporter": "^0.2.2",
      "protractor": "~5.3.0",
      "ts-node": "~5.0.1",
      "tslint": "~5.9.1",
      "typescript": "~2.7.2",
      "webpack-cli": "^3.0.8"
    }
  }
  </code></pre>
  
  <p><strong>webpack.server.config.js</strong></p>
  
  <pre><code>const path = require('path');
  const webpack = require('webpack');
  
  module.exports = {
    entry: {  server: './server.ts' },
    resolve: { extensions: ['.js', '.ts'] },
    target: 'node',
    // this makes sure we include node_modules and other 3rd party libraries
    externals: [/(node_modules|main\..*\.js)/],
    output: {
      path: path.join(__dirname, 'dist'),
      filename: '[name].js'
    },
    module: {
      rules: [
        { test: /\.ts$/, loader: 'ts-loader' }
      ]
    },
    plugins: [
      // Temporary Fix for issue: https://github.com/angular/angular/issues/11580
      // for "WARNING Critical dependency: the request of a dependency is an expression"
      new webpack.ContextReplacementPlugin(
        /(.+)?angular(\\|\/)core(.+)?/,
        path.join(__dirname, 'src'), // location of your src
        {} // a map of your routes
      ),
      new webpack.ContextReplacementPlugin(
        /(.+)?express(\\|\/)(.+)?/,
        path.join(__dirname, 'src'),
        {}
      )
    ]
  }
  </code></pre>
  
  <p>I successfully created the build of the app using the command 'npm run build:ssr',but when I trying to start the server 'npm run serve:ssr',it shows above error.can anyone please help me to solve this issue?   </p>