angular-cli: angular universal server.ts fails
Versions
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 6.0.0
Node: 8.10.0
OS: linux x64
Angular: 5.2.9
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.6.0
@angular-devkit/build-angular 0.6.0
@angular-devkit/build-optimizer 0.6.0
@angular-devkit/core 0.0.29
@angular-devkit/schematics 0.6.0
@angular/cli 6.0.0
@angular/platform-server 6.0.0
@ngtools/webpack 6.0.0
@schematics/angular 0.6.0
@schematics/update 0.6.0
rxjs 5.5.7
typescript 2.4.2
webpack 4.6.0
-----------------------------------------------------------------------------
Distributor ID: Debian
Description: Debian GNU/Linux 9.4 (stretch)
Release: 9.4
Codename: stretch
Repro steps
- Follow Angular Universal Integration at https://github.com/angular/angular-cli/wiki/stories-universal-rendering
Observed behavior
project builds and the server runs, but when entering to localhost:4000 an error is throw
Node server listening on http://localhost:4000
ERROR { TypeError: StaticInjectorError[function (){} -> InjectionToken DocumentToken]:
StaticInjectorError(Platform: core)[function (){} -> InjectionToken DocumentToken]:
Right-hand side of 'instanceof' is not an object
at bt (/home/username/Desktop/projectname/dist/server.js:1239:87385)
at yt (/home/username/Desktop/projectname/dist/server.js:1239:87260)
at ir (/home/username/Desktop/projectname/dist/server.js:1239:110031)
at He.insertToken (/home/username/Desktop/projectname/dist/server.js:1239:86646)
at /home/username/Desktop/projectname/dist/server.js:1239:89924
at Mt (/home/username/Desktop/projectname/dist/server.js:1239:89939)
at Ke (/home/username/Desktop/projectname/dist/server.js:1239:84920)
at Object.parse (/home/username/Desktop/projectname/dist/server.js:1239:83906)
at Object.t.createDocument (/home/username/Desktop/projectname/dist/server.js:3180:76668)
at Object.t.createWindow (/home/username/Desktop/projectname/dist/server.js:3180:76772)
ngTempTokenPath: null,
ngTokenPath:
[ 'function (){}',
e { _desc: 'DocumentToken', ngMetadataName: 'InjectionToken' } ] }
Unhandled Promise rejection: StaticInjectorError[function (){} -> InjectionToken DocumentToken]:
StaticInjectorError(Platform: core)[function (){} -> InjectionToken DocumentToken]:
Right-hand side of 'instanceof' is not an object ; Zone: <root> ; Task: Promise.then ; Value: { TypeError: StaticInjectorError[function (){} -> InjectionToken DocumentToken]:
StaticInjectorError(Platform: core)[function (){} -> InjectionToken DocumentToken]:
Right-hand side of 'instanceof' is not an object
at bt (/home/username/Desktop/projectname/dist/server.js:1239:87385)
at yt (/home/username/Desktop/projectname/dist/server.js:1239:87260)
at ir (/home/username/Desktop/projectname/dist/server.js:1239:110031)
at He.insertToken (/home/username/Desktop/projectname/dist/server.js:1239:86646)
at /home/username/Desktop/projectname/dist/server.js:1239:89924
at Mt (/home/username/Desktop/projectname/dist/server.js:1239:89939)
at Ke (/home/username/Desktop/projectname/dist/server.js:1239:84920)
at Object.parse (/home/username/Desktop/projectname/dist/server.js:1239:83906)
at Object.t.createDocument (/home/username/Desktop/projectname/dist/server.js:3180:76668)
at Object.t.createWindow (/home/username/Desktop/projectname/dist/server.js:3180:76772)
ngTempTokenPath: null,
ngTokenPath:
[ 'function (){}',
e { _desc: 'DocumentToken', ngMetadataName: 'InjectionToken' } ] } TypeError: StaticInjectorError[function (){} -> InjectionToken DocumentToken]:
StaticInjectorError(Platform: core)[function (){} -> InjectionToken DocumentToken]:
Right-hand side of 'instanceof' is not an object
at bt (/home/username/Desktop/projectname/dist/server.js:1239:87385)
at yt (/home/username/Desktop/projectname/dist/server.js:1239:87260)
at ir (/home/username/Desktop/projectname/dist/server.js:1239:110031)
at He.insertToken (/home/username/Desktop/projectname/dist/server.js:1239:86646)
at /home/username/Desktop/projectname/dist/server.js:1239:89924
at Mt (/home/username/Desktop/projectname/dist/server.js:1239:89939)
at Ke (/home/username/Desktop/projectname/dist/server.js:1239:84920)
at Object.parse (/home/username/Desktop/projectname/dist/server.js:1239:83906)
at Object.t.createDocument (/home/username/Desktop/projectname/dist/server.js:3180:76668)
at Object.t.createWindow (/home/username/Desktop/projectname/dist/server.js:3180:76772)
Desired behavior
Is expected to render the application
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 15
- Comments: 24 (1 by maintainers)
Setting
mode: 'none'
in webpack config solved this issue for me. But the bundle sized increased from 1.6Mb to 4.8MbWebpack in
mode: "production"
optimizes the output with certain defaults (If you don’t set a mode, it is “production” by default). One optimization is including UglifyJsPlugin which will mangle class and functions names and break injection. You can (and should) keep production mode for all other optimizations. All of the following edits are made to yourwebpack.server.config.js
file.and just disable UglifyJs
This does NOT affect the client side js bundles sent to the browser, just your server.js file running in node. However, if you really want the server.js to be minimized as well (I doubt that will have any impact on the performance at all) you can manually add UglifyJsPlugin back and enable
keep_classnames
andkeep_fnames
options.Here my configuration for universal:
Like this we have an optimization for the build & the good environment.
yes, just confirming this seems to be a fix. bundle size isn’t that great (BIG project pumping out 8.4mb) but I suppose it doesn’t matter seeing that it’s running on the server. I guess file size isn’t as big an issue because the client isn’t downloading it. but still, if uglifying and compiling server code is going to improve server performance production compiling should be an option.
@epgeroy https://webpack.js.org/concepts/mode/
In addition, this are the scripts I’m using (most of them are boilerplate code posted on https://github.com/angular/angular-cli/wiki/stories-universal-rendering)
angular.json architect->server section
package.json relevant scripts
using
npm run build:ssr && npm run serve:ssr
to build and runmain.server.ts
app.server.module.ts
tsconfig.server.json
webpack.server.config.js