Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animations causing problems with SSR: document is not defined #15098

Closed
kukjevov opened this issue Mar 12, 2017 · 30 comments
Closed

Animations causing problems with SSR: document is not defined #15098

kukjevov opened this issue Mar 12, 2017 · 30 comments
Assignees
Labels
area: animations area: server Issues related to server-side rendering type: bug/fix

Comments

@kukjevov
Copy link
Contributor

kukjevov commented Mar 12, 2017

I'm submitting a ... (check one with "x")

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
When i use animations in HTML, SSR crashes.

I have my animation defined and used in html like this

<div [@flyIn]></div>

I have browser module which imports import {BrowserAnimationsModule} from '@angular/platform-browser/animations';

I received error on server
Error: Found the synthetic property @flyin. Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.

When i import in server module import {NoopAnimationsModule} from '@angular/platform-browser/animations';
i receive another error which cause crash of server.

EXCEPTION: document is not defined
ORIGINAL STACKTRACE:
ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (F:\git\ng-universal-demo\dist\server.js:31873:72)
at createElement (F:\git\ng-universal-demo\dist\server.js:9067:23)
at createViewNodes (F:\git\ng-universal-demo\dist\server.js:11533:44)
at createRootView (F:\git\ng-universal-demo\dist\server.js:11480:5)
at Object.createProdRootView [as createRootView] (F:\git\ng-universal-demo\dist\server.js:12049:12)
at ComponentFactory_.create (F:\git\ng-universal-demo\dist\server.js:9485:46)
at ApplicationRef_.bootstrap (F:\git\ng-universal-demo\dist\server.js:4862:57)
at F:\git\ng-universal-demo\dist\server.js:4657:89
at Array.forEach (native)
at PlatformRef_.moduleDoBootstrap (F:\git\ng-universal-demo\dist\server.js:4657:42)
at F:\git\ng-universal-demo\dist\server.js:4620:27
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59567:26)
at Object.onInvoke (F:\git\ng-universal-demo\dist\server.js:4017:37)
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59566:32)
at Zone.run (F:\git\ng-universal-demo\dist\server.js:59359:43)
Unhandled Promise rejection: document is not defined ; Zone: ; Task: Promise.then ; Value: { ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (F:\git\ng-universal-demo\dist\server.js:31873:72)
at createElement (F:\git\ng-universal-demo\dist\server.js:9067:23)
at createViewNodes (F:\git\ng-universal-demo\dist\server.js:11533:44)
at createRootView (F:\git\ng-universal-demo\dist\server.js:11480:5)
at Object.createProdRootView [as createRootView] (F:\git\ng-universal-demo\dist\server.js:12049:12)
at ComponentFactory
.create (F:\git\ng-universal-demo\dist\server.js:9485:46)
at ApplicationRef_.bootstrap (F:\git\ng-universal-demo\dist\server.js:4862:57)
at F:\git\ng-universal-demo\dist\server.js:4657:89
at Array.forEach (native)
at PlatformRef_._moduleDoBootstrap (F:\git\ng-universal-demo\dist\server.js:4657:42)
at F:\git\ng-universal-demo\dist\server.js:4620:27
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59567:26)
at Object.onInvoke (F:\git\ng-universal-demo\dist\server.js:4017:37)
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59566:32)
at Zone.run (F:\git\ng-universal-demo\dist\server.js:59359:43)
__zone_symbol__currentTask:
ZoneTask {
zone:
Zone {
_properties: {},
parent: null,
name: '',
zoneDelegate: [Object] },
runCount: 0,
zoneDelegates: null,
state: 'notScheduled',
type: 'microTask',
source: 'Promise.then',
data: undefined,
scheduleFn: undefined,
cancelFn: null,
callback: [Function],
invoke: [Function] } } ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (F:\git\ng-universal-demo\dist\server.js:31873:72)
at createElement (F:\git\ng-universal-demo\dist\server.js:9067:23)
at createViewNodes (F:\git\ng-universal-demo\dist\server.js:11533:44)
at createRootView (F:\git\ng-universal-demo\dist\server.js:11480:5)
at Object.createProdRootView [as createRootView] (F:\git\ng-universal-demo\dist\server.js:12049:12)
at ComponentFactory
.create (F:\git\ng-universal-demo\dist\server.js:9485:46)
at ApplicationRef
.bootstrap (F:\git\ng-universal-demo\dist\server.js:4862:57)
at F:\git\ng-universal-demo\dist\server.js:4657:89
at Array.forEach (native)
at PlatformRef
.moduleDoBootstrap (F:\git\ng-universal-demo\dist\server.js:4657:42)
at F:\git\ng-universal-demo\dist\server.js:4620:27
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59567:26)
at Object.onInvoke (F:\git\ng-universal-demo\dist\server.js:4017:37)
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59566:32)
at Zone.run (F:\git\ng-universal-demo\dist\server.js:59359:43)
{ Error: Uncaught (in promise): ReferenceError: document is not defined
ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (F:\git\ng-universal-demo\dist\server.js:31873:72)
at createElement (F:\git\ng-universal-demo\dist\server.js:9067:23)
at createViewNodes (F:\git\ng-universal-demo\dist\server.js:11533:44)
at createRootView (F:\git\ng-universal-demo\dist\server.js:11480:5)
at Object.createProdRootView [as createRootView] (F:\git\ng-universal-demo\dist\server.js:12049:12)
at ComponentFactory
.create (F:\git\ng-universal-demo\dist\server.js:9485:46)
at ApplicationRef.bootstrap (F:\git\ng-universal-demo\dist\server.js:4862:57)
at F:\git\ng-universal-demo\dist\server.js:4657:89
at Array.forEach (native)
at PlatformRef.moduleDoBootstrap (F:\git\ng-universal-demo\dist\server.js:4657:42)
at F:\git\ng-universal-demo\dist\server.js:4620:27
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59567:26)
at Object.onInvoke (F:\git\ng-universal-demo\dist\server.js:4017:37)
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59566:32)
at Zone.run (F:\git\ng-universal-demo\dist\server.js:59359:43)
at resolvePromise (F:\git\ng-universal-demo\dist\server.js:59898:31) []
at resolvePromise (F:\git\ng-universal-demo\dist\server.js:59869:17) []
at F:\git\ng-universal-demo\dist\server.js:59946:17 []
at Zone.runTask (F:\git\ng-universal-demo\dist\server.js:59399:47) [ => ]
at drainMicroTaskQueue (F:\git\ng-universal-demo\dist\server.js:59779:35) []
at ZoneTask.invoke (F:\git\ng-universal-demo\dist\server.js:59657:25) []
at data.args.(anonymous function) (F:\git\ng-universal-demo\dist\server.js:60752:25) []
at FSReqWrap.oncomplete (fs.js:114:15) []
zone_symbol__error:
{ Error: Uncaught (in promise): ReferenceError: document is not defined
ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (F:\git\ng-universal-demo\dist\server.js:31873:72)
at createElement (F:\git\ng-universal-demo\dist\server.js:9067:23)
at createViewNodes (F:\git\ng-universal-demo\dist\server.js:11533:44)
at createRootView (F:\git\ng-universal-demo\dist\server.js:11480:5)
at Object.createProdRootView [as createRootView] (F:\git\ng-universal-demo\dist\server.js:12049:12)
at ComponentFactory
.create (F:\git\ng-universal-demo\dist\server.js:9485:46)
at ApplicationRef
.bootstrap (F:\git\ng-universal-demo\dist\server.js:4862:57)
at F:\git\ng-universal-demo\dist\server.js:4657:89
at Array.forEach (native)
at PlatformRef
._moduleDoBootstrap (F:\git\ng-universal-demo\dist\server.js:4657:42)
at F:\git\ng-universal-demo\dist\server.js:4620:27
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59567:26)
at Object.onInvoke (F:\git\ng-universal-demo\dist\server.js:4017:37)
at ZoneDelegate.invoke (F:\git\ng-universal-demo\dist\server.js:59566:32)
at Zone.run (F:\git\ng-universal-demo\dist\server.js:59359:43)
at resolvePromise (F:\git\ng-universal-demo\dist\server.js:59898:31) []
at resolvePromise (F:\git\ng-universal-demo\dist\server.js:59869:17) []
at F:\git\ng-universal-demo\dist\server.js:59946:17 []
at Zone.runTask (F:\git\ng-universal-demo\dist\server.js:59399:47) [ => ]
at drainMicroTaskQueue (F:\git\ng-universal-demo\dist\server.js:59779:35) []
at ZoneTask.invoke (F:\git\ng-universal-demo\dist\server.js:59657:25) []
at data.args.(anonymous function) (F:\git\ng-universal-demo\dist\server.js:60752:25) []
at FSReqWrap.oncomplete (fs.js:114:15) []

Expected behavior
When i use animations in HTML, it will have no influence on SSR.

Minimal reproduction of the problem with instructions

  • Angular version: >=4.0.0-rc.3
  • Browser: [all]
  • Language: [TypeScript 2.2.1]

  • Node (for AoT issues): node --version = v7.7.1

@kbarralon
Copy link

Same error with the same config.

@michaelwaihenya
Copy link

+1

@kukjevov
Copy link
Contributor Author

kukjevov commented Mar 13, 2017

Here is repository where last commit simulates error:

https://github.com/kukjevov/ng-universal-demo

It looks like that this https://github.com/angular/angular/pull/14880/files PR started this bug, or it is somehow connected to this.

@chuckjaz, @tbosch can you look at this :)?

Thank you

@nicky-lenaers
Copy link

Same error happens when using in Universal context

@feloy
Copy link
Contributor

feloy commented Mar 13, 2017

I got the same error upgrading to v4-rc.3 when using material2-beta2 (and the Menu component which uses animations).
I had to import the BrowserAnimationsModule from the AppModule and it worked again. (or NoopAnimationsModule if you do not want animations).

@kukjevov
Copy link
Contributor Author

kukjevov commented Mar 13, 2017

@feloy Are you using Server Side Rendering (SSR) aka Universal ? Because this error happens in NodeJs environment, not in browser, but during rendering on server side.

I have BrowserAnimationsModule on browser side and it is working, this is not a problem :).

@feloy
Copy link
Contributor

feloy commented Mar 13, 2017

@kukjevov Well, no, I got the same error (Error: Found the synthetic property ... Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.) but in the browser. My answer were not directly directed to you, but placed here if somebody had the same error in my context (using Material in the browser), because your description helped me resolve my problem.

It was not very clear, sorry ;)

@kukjevov kukjevov reopened this Mar 13, 2017
@kukjevov
Copy link
Contributor Author

No problem :), but that server side part is a bug :).

@DzmitryShylovich
Copy link
Contributor

Probably duplicate of #14784

@kukjevov
Copy link
Contributor Author

It is not duplicate :). Recomended solution in #14784 cause error mention above document is not defined.

@chuckjaz chuckjaz added regression Indicates than the issue relates to something that worked in a previous version type: bug/fix area: animations labels Mar 13, 2017
@MikeRyanDev
Copy link
Member

Ran into the same problem. If you need a fix you can patch the server renderer to ignore the error:

import { ɵServerRendererFactory2 } from '@angular/platform-server';


const createRenderer = ɵServerRendererFactory2.prototype.createRenderer;

ɵServerRendererFactory2.prototype.createRenderer = function () {
  const result = createRenderer.apply(this, arguments);

  const setProperty = result.setProperty;

  result.setProperty = function () {
    try {
      setProperty.apply(this, arguments);
    } catch (e) {
      if (e.message.indexOf('Found the synthetic') === -1) {
        throw e;
      }
    }
  };

  return result;
}

@vikerman vikerman self-assigned this Mar 13, 2017
@vikerman vikerman added the area: server Issues related to server-side rendering label Mar 13, 2017
@vikerman vikerman added severity3: broken and removed regression Indicates than the issue relates to something that worked in a previous version labels Mar 14, 2017
@vikerman vikerman added this to the 4.0.0-blockers milestone Mar 14, 2017
@yfain
Copy link

yfain commented Mar 14, 2017

I'm not using SSR, but Angular Material Beta.2 gives me the following error with 4.00-rc.2 when I run ng serve (this app works fine in Angular 2):

Error: Found the synthetic property @transformPlaceholder. Please include either "BrowserAnimationsModule" or "NoopAnimationsModule" in your application.
at checkNoSyntheticProp (http://localhost:4200/vendor.bundle.js:21462:15) [angular]
at DefaultDomRenderer2.setProperty (http://localhost:4200/vendor.bundle.js:21430:9) [angular]
at DebugRenderer2.setProperty (http://localhost:4200/vendor.bundle.js:12813:23) [angular]
at setElementProperty (http://localhost:4200/vendor.bundle.js:9272:19) [angular]
at checkAndUpdateElementValue (http://localhost:4200/vendor.bundle.js:9188:13) [angular]
at checkAndUpdateElementInline (http://localhost:4200/vendor.bundle.js:9128:24) [angular]
at checkAndUpdateNodeInline (http://localhost:4200/vendor.bundle.js:11679:23) [angular]
at checkAndUpdateNode (http://localhost:4200/vendor.bundle.js:11654:16) [angular]
at debugCheckAndUpdateNode (http://localhost:4200/vendor.bundle.js:12276:59) [angular]
at debugCheckRenderNodeFn (http://localhost:4200/vendor.bundle.js:12255:13) [angular]
at Object.eval [as updateRenderer] (/MdSelectModule/MdSelect/component.ngfactory.js:455:5) [angular]
at Object.debugUpdateRenderer [as updateRenderer] (http://localhost:4200/vendor.bundle.js:12240:21) [angular]
at checkAndUpdateView (http://localhost:4200/vendor.bundle.js:11626:14) [angular]
at callViewAction (http://localhost:4200/vendor.bundle.js:11938:17) [angular]

@vikerman
Copy link
Contributor

@yfain - Unrelated to this bug.

You should include BrwoserAnimationModule in your app module as the error indicates.

@yfain
Copy link

yfain commented Mar 14, 2017

Yes, I actually fixed this issue by adding import {BrowserAnimationsModule} from '@angular/platform-browser/animations'; to the app.module.

SamVerschueren pushed a commit to SamVerschueren/angular that referenced this issue Mar 18, 2017
…efault (angular#15131)

This is so that server side rendering does not throw an exception when it encounters animations on the server side and does not need the user to explicitly setup NoopAnimationsModule in their app server module.

Fixes angular#15098, angular#14784.

PR Close angular#15131
@RockNHawk
Copy link

RockNHawk commented Apr 15, 2017

I sitll have the same Error in Angualr 4.0.2 Universal

ZoneAwarePromise {
__zone_symbol__state: 0,
__zone_symbol__value:
ReferenceError: document is not defined
at ViewportRuler._cacheViewportGeometry (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:43537:30)
at new ViewportRuler (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:43483:14)
at Object.VIEWPORT_RULER_PROVIDER_FACTORY (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:43556:27)
at AppServerModuleInjector.get [as _ɵh_81] (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:171879:94)
at AppServerModuleInjector.getInternal (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:172711:24)
at AppServerModuleInjector.NgModuleInjector.get (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:3980:44)
at resolveDep (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:11354:45)
at createClass (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:11223:32)
at createDirectiveInstance (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:11051:37)
at createViewNodes (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12401:49)
at callViewAction (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12771:13)
at execComponentViewsAction (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12710:13)
at createViewNodes (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12428:5)
at createRootView (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12306:5)
at Object.createProdRootView [as createRootView] (/Users/laurel/git/Nanhua.IOT/IOT-master/src/client2/dist/server.js:12884:12) },

@szykov
Copy link

szykov commented Apr 16, 2017

Same thing for me:

Exception: Call to Node module failed with error: ReferenceError: document is not defined
at DefaultDomRenderer2.selectRootElement (C:\Users\Administrator\Desktop\ng2-cv-blog\src\SPA\rendered\dist\app-server.js:18047:72)
at createElement (C:\Users\Administrator\Desktop\ng2-cv-blog\src\SPA\rendered\dist\app-server.js:9587:23)

     /**
     * @param {?} selectorOrNode
     * @return {?}
     */
    DefaultDomRenderer2.prototype.selectRootElement = function (selectorOrNode) {
        var /** @type {?} */ el = typeof selectorOrNode === 'string' ? document.querySelector(selectorOrNode) :
            selectorOrNode;
        if (!el) {
            throw new Error("The selector \"" + selectorOrNode + "\" did not match any elements");
        }
        el.textContent = '';
        return el;
    };

@ysus
Copy link

ysus commented Apr 21, 2017

update for the last release but sitll i have the same errorr (Angular 4.0.3) Universal

ERROR { ReferenceError: document is not defined at DefaultDomRenderer2.selectRootElement (C:\src2\evertonrobertoauler\cli-universal-demo-master\node_modules\@angular\platform-browser\bundles\platform-browser.umd.js:2821:72)

repo:
https://github.com/evertonrobertoauler/cli-universal-demo

@qdouble
Copy link

qdouble commented Apr 28, 2017

I still see this error in 4.1.0... @MikeRyanDev where do you apply that patch?

@ysus
Copy link

ysus commented Apr 28, 2017

@NelsonLamprecht
Copy link

I still see this issue with angular universal running 4.0.0. Does 4.1 fix this?

@BillyQin
Copy link

I have Same error in 4.1.0,how to resolve this problem? thanks @kukjevov
I don't know how to use ɵServerRendererFactory2 , ɵServerRendererFactory2 can replace document in node?thanks @MikeRyanDev

@sudippal
Copy link

@BillyQin I just figured it out after the long day search, hope this helps

// imports
import { NgModule, NgModuleFactory, NgModuleFactoryLoader, RendererFactory2, NgZone } from '@angular/core';
import { ServerModule, ɵServerRendererFactory2 } from '@angular/platform-server';
import { ɵAnimationEngine } from '@angular/animations/browser';
import { NoopAnimationsModule, ɵAnimationRendererFactory } from '@angular/platform-browser/animations';

// declarations
export function instantiateServerRendererFactory(
  renderer: RendererFactory2, engine: ɵAnimationEngine, zone: NgZone) {
   return new ɵAnimationRendererFactory(renderer, engine, zone);
}

const createRenderer = ɵServerRendererFactory2.prototype.createRenderer;
ɵServerRendererFactory2.prototype.createRenderer = function () {
  const result = createRenderer.apply(this, arguments);
  const setProperty = result.setProperty;
  result.setProperty = function () {
    try {
      setProperty.apply(this, arguments);
    } catch (e) {
      if (e.message.indexOf('Found the synthetic') === -1) {
        throw e;
      }
    }
  };
  return result;
}

export const SERVER_RENDER_PROVIDERS = [
  {
    provide: RendererFactory2,
    useFactory: instantiateServerRendererFactory,
    deps: [ɵServerRendererFactory2, ɵAnimationEngine, NgZone]
  }
];

// Module ref.
@NgModule({
  imports: [
    ServerModule,
    NoopAnimationsModule
  ],
  bootstrap: [.....],
  providers: [
    { provide: ........, useClass: ...... },
    SERVER_RENDER_PROVIDERS
  ]
})

@jdlaping
Copy link

thank you! this fixed my issue.

asnowwolf pushed a commit to asnowwolf/angular that referenced this issue Aug 11, 2017
…efault (angular#15131)

This is so that server side rendering does not throw an exception when it encounters animations on the server side and does not need the user to explicitly setup NoopAnimationsModule in their app server module.

Fixes angular#15098, angular#14784.

PR Close angular#15131
juleskremer pushed a commit to juleskremer/angular that referenced this issue Aug 28, 2017
…efault (angular#15131)

This is so that server side rendering does not throw an exception when it encounters animations on the server side and does not need the user to explicitly setup NoopAnimationsModule in their app server module.

Fixes angular#15098, angular#14784.

PR Close angular#15131
@kakins
Copy link

kakins commented Sep 12, 2017

I am still experiencing this issue, and not really sure what the ultimate resolution was for this thread. Angular 4.2.5.

@shamoh19
Copy link

shamoh19 commented Oct 24, 2017

@sudippal Hi can you please elaborate your solution? I am facing the same issue while server side rendering to serve a fully generated HTML page in server.ts

> ang_bs@0.0.0 start /Applications/Workspace/Angular/Ang_Bs
   > ts-node src/server.ts

   listening on http://localhost:4001!
   ERROR { ReferenceError: document is not defined
       at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
       at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
       at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
       at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
       at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
       at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
       at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
       at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
       at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
       at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1
   __zone_symbol__currentTask:
   ZoneTask {
       _zone:
       Zone {
           _properties: [Object],
           _parent: [Object],
           _name: 'angular',
           _zoneDelegate: [Object] },
       runCount: 0,
       _zoneDelegates: null,
       _state: 'notScheduled',
       type: 'microTask',
       source: 'Promise.then',
       data: undefined,
       scheduleFn: undefined,
       cancelFn: null,
       callback: [Function],
       invoke: [Function] } }
   Unhandled Promise rejection: document is not defined ; Zone: <root> ; Task: Promise.then ; Value: { ReferenceError: document is not defined
       at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
       at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
       at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
       at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
       at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
       at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
       at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
       at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
       at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
       at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1
   __zone_symbol__currentTask:
   ZoneTask {
       _zone:
       Zone {
           _properties: {},
           _parent: null,
           _name: '<root>',
           _zoneDelegate: [Object] },
       runCount: 0,
       _zoneDelegates: null,
       _state: 'notScheduled',
       type: 'microTask',
       source: 'Promise.then',
       data: undefined,
       scheduleFn: undefined,
       cancelFn: null,
       callback: [Function],
       invoke: [Function] } } ReferenceError: document is not defined
       at DefaultDomRenderer2.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js:2818:72)
       at BaseAnimationRenderer.selectRootElement (/Applications/Workspace/Angular/Ang_Bs/packages/platform-browser/animations/src/animation_renderer.ts:166:62)
       at createElement (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9220:23)
       at createViewNodes (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/view/view.ts:320:1)
       at createRootView (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12114:5)
       at Object.createProdRootView [as createRootView] (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:12788:12)
       at ComponentFactory_.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:9893:46)
       at ComponentFactoryBoundToModule.create (/Applications/Workspace/Angular/Ang_Bs/node_modules/@angular/core/bundles/core.umd.js:3365:29)
       at ApplicationRef_.bootstrap (/Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:670:1)
       at /Applications/Workspace/Angular/Ang_Bs/packages/core/src/application_ref.ts:415:1

@UTGuy
Copy link

UTGuy commented Nov 5, 2017

To solve the SSR issue I just created 3 modules - a Common module, a Browser module, and a Server module...

app-material.module.ts (common one)

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCalendar } from '@angular/material';

@NgModule({
    imports: [
        CommonModule,
        //Add imorted material items here - ex. MatCalendar
    ],
    exports: [
        //Add imorted material items here - ex. MatCalendar
    ]
})
export class AppMaterialModule { }

app-material.module.server.ts (import the common one here)

import { NgModule } from '@angular/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { AppMaterialModule } from "./app-material.module";

@NgModule({
    imports: [
        AppMaterialModule,
        NoopAnimationsModule
    ],
    exports: [
        AppMaterialModule,
        NoopAnimationsModule
    ]
})
export class AppMaterialServerModule { }

app-material.module.browser.ts (import the common one here)

import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppMaterialModule } from "./app-material.module";

@NgModule({
    imports: [
        AppMaterialModule,
        BrowserAnimationsModule
    ],
    exports: [
        AppMaterialModule,
        BrowserAnimationsModule
    ]
})
export class AppMaterialBrowserModule { }

Now just import the one you need into your main module

@atariman5000
Copy link

I too had this error. However, I was able to resolve the error by importing import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; into the "app.module.browser.ts" file.

`import { NgModule } from '@angular/core';
import { BrowserModule, DOCUMENT } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// App
import { AppModuleShared } from './app.module.shared';
import { AppComponent } from './pages/shared/app/app.component';

@NgModule({
bootstrap: [ AppComponent ],
imports: [
BrowserAnimationsModule,
BrowserModule,
AppModuleShared
],
providers: [
{ provide: 'BASE_URL', useFactory: getBaseUrl }
]
})
export class AppModule {
}

export function getBaseUrl() {
return document.getElementsByTagName('base')[0].href;
}
`

@viraya
Copy link

viraya commented Apr 19, 2018

In my case i did add the import of BrowserAnimationsModule to the top of app.module.ts but i forgot to add it to the imports array. after adding it my issue was fixed.

@tiberiumihai
Copy link

In my case I was importing NoopAnimationsModule before BrowserAnimationsModule like this:

NoopAnimationsModule,
BrowserAnimationsModule,

Just switch the order so that BrowserAnimationsModule is first:

BrowserAnimationsModule,
NoopAnimationsModule,

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: animations area: server Issues related to server-side rendering type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.