Holiday Notice: Support will be provided on a limited scale from December 24th, 2024, to January 2nd, 2025. Happy holidays and a wonderful New Year!


Topic: mdbootstrap V9 SSR ReferenceError: KeyboardEvent is not defined

yuenkai free asked 4 years ago


I trying MDBootStrap V9 for SSR. and having error ReferenceError: KeyboardEvent is not defined when i run ==> npm run serve:ssr

  1. Create new angular project
  2. Update styles and scripts arrays in angular.json file
  3. Update the app.module.ts
  4. npm i --save chart.js @types/chart.js easy-pie-chart hammerjs screenfull @fortawesome/fontawesome-free animate.css
  5. ng add @nguniversal/express-engine
  6. npm run build:ssr
  7. npm run serve:ssr ==> Error ReferenceError: KeyboardEvent is not defined

  8. try to run ng serve => success

  9. try to remove MDBootstrap module from app.module.ts and run npm run build:ssr | npm run serve:ssr, it run successfully

is MDBootStrap V9 compatible with Angular Universal?

app.module.ts

import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { MDBSpinningPreloader, MDBBootstrapModulesPro, ToastModule } from 'ng-uikit-pro-standard'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

@NgModule({ declarations: [AppComponent], imports: [ BrowserModule.withServerTransition({ appId: 'serverApp' }), AppRoutingModule, BrowserAnimationsModule, ToastModule.forRoot(), MDBBootstrapModulesPro.forRoot(), AppRoutingModule, FormsModule, ReactiveFormsModule ], providers: [MDBSpinningPreloader],

bootstrap: [AppComponent]

}) export class AppModule {}

package.json { "name": "client-app", "version": "0.0.0", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "dev:ssr": "ng run ClientApp:serve-ssr", "serve:ssr": "node dist/ClientApp/server/main.js", "build:ssr": "ng build --prod && ng run ClientApp:server:production", "prerender": "ng run ClientApp:prerender" }, "private": true, "dependencies": { "@angular/animations": "~9.0.5", "@angular/common": "~9.0.5", "@angular/compiler": "~9.0.5", "@angular/core": "~9.0.5", "@angular/forms": "~9.0.5", "@angular/platform-browser": "~9.0.5", "@angular/platform-browser-dynamic": "~9.0.5", "@angular/platform-server": "^9.0.6", "@angular/router": "~9.0.5", "@fortawesome/fontawesome-free": "^5.12.1", "@nguniversal/express-engine": "^9.0.2", "@nguniversal/module-map-ngfactory-loader": "^8.2.6", "@types/chart.js": "^2.9.15", "animate.css": "^3.7.2", "chart.js": "^3.0.0-alpha", "easy-pie-chart": "^2.1.7", "express": "^4.17.1", "hammerjs": "^2.0.8", "rxjs": "~6.5.4", "screenfull": "^5.0.2", "ts-loader": "^6.2.1", "tslib": "^1.10.0", "webpack-node-externals": "^1.7.2", "zone.js": "~0.10.2", "ng-uikit-pro-standard": "file:ng-uikit-pro-standard-9.0.0.tgz" }, "devDependencies": { "@angular-devkit/build-angular": "~0.900.5", "@angular/cli": "~9.0.5", "@angular/compiler-cli": "~9.0.5", "@angular/language-service": "~9.0.5", "@nguniversal/builders": "^9.0.2", "@types/express": "^4.17.0", "@types/node": "^12.11.1", "@types/jasmine": "~3.5.0", "@types/jasminewd2": "~2.0.3", "codelyzer": "^5.1.2", "jasmine-core": "~3.5.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~4.3.0", "karma-chrome-launcher": "~3.1.0", "karma-coverage-istanbul-reporter": "~2.1.0", "karma-jasmine": "~2.0.1", "karma-jasmine-html-reporter": "^1.4.2", "protractor": "~5.4.3", "ts-node": "~8.3.0", "tslint": "~5.18.0", "typescript": "~3.7.5" } }

angular.json

{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "version": 1, "newProjectRoot": "projects", "projects": { "ClientApp": { "projectType": "application", "schematics": { "@schematics/angular:component": { "style": "scss" } }, "root": "", "sourceRoot": "src", "prefix": "app", "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": { "outputPath": "dist/ClientApp/browser", "index": "src/index.html", "main": "src/main.ts", "polyfills": "src/polyfills.ts", "tsConfig": "tsconfig.app.json", "aot": true, "assets": ["src/favicon.ico", "src/assets"], "styles": [ "node_modules/@fortawesome/fontawesome-free/scss/fontawesome.scss", "node_modules/@fortawesome/fontawesome-free/scss/solid.scss", "node_modules/@fortawesome/fontawesome-free/scss/regular.scss", "node_modules/@fortawesome/fontawesome-free/scss/brands.scss", "node_modules/ng-uikit-pro-standard/assets/scss/bootstrap/bootstrap.scss", "node_modules/ng-uikit-pro-standard/assets/scss/mdb.scss", "node_modules/animate.css/animate.css", "src/styles.scss" ],

                    "scripts": [
                        "node_modules/chart.js/dist/Chart.js",
                        "node_modules/easy-pie-chart/dist/easypiechart.js",
                        "node_modules/screenfull/dist/screenfull.js",
                        "node_modules/hammerjs/hammer.min.js"
                    ]
                },
                "configurations": {
                    "production": {
                        "fileReplacements": [
                            {
                                "replace": "src/environments/environment.ts",
                                "with": "src/environments/environment.prod.ts"
                            }
                        ],
                        "optimization": true,
                        "outputHashing": "all",
                        "sourceMap": false,
                        "extractCss": true,
                        "namedChunks": false,
                        "extractLicenses": true,
                        "vendorChunk": false,
                        "buildOptimizer": true,
                        "budgets": [
                            {
                                "type": "initial",
                                "maximumWarning": "2mb",
                                "maximumError": "5mb"
                            },
                            {
                                "type": "anyComponentStyle",
                                "maximumWarning": "6kb",
                                "maximumError": "10kb"
                            }
                        ]
                    }
                }
            },
            "serve": {
                "builder": "@angular-devkit/build-angular:dev-server",
                "options": {
                    "browserTarget": "ClientApp:build"
                },
                "configurations": {
                    "production": {
                        "browserTarget": "ClientApp:build:production"
                    }
                }
            },
            "extract-i18n": {
                "builder": "@angular-devkit/build-angular:extract-i18n",
                "options": {
                    "browserTarget": "ClientApp:build"
                }
            },
            "test": {
                "builder": "@angular-devkit/build-angular:karma",
                "options": {
                    "main": "src/test.ts",
                    "polyfills": "src/polyfills.ts",
                    "tsConfig": "tsconfig.spec.json",
                    "karmaConfig": "karma.conf.js",
                    "assets": ["src/favicon.ico", "src/assets"],
                    "styles": ["src/styles.scss"],
                    "scripts": []
                }
            },
            "lint": {
                "builder": "@angular-devkit/build-angular:tslint",
                "options": {
                    "tsConfig": ["tsconfig.app.json", "tsconfig.spec.json", "e2e/tsconfig.json"],
                    "exclude": ["**/node_modules/**"]
                }
            },
            "e2e": {
                "builder": "@angular-devkit/build-angular:protractor",
                "options": {
                    "protractorConfig": "e2e/protractor.conf.js",
                    "devServerTarget": "ClientApp:serve"
                },
                "configurations": {
                    "production": {
                        "devServerTarget": "ClientApp:serve:production"
                    }
                }
            },
            "server": {
                "builder": "@angular-devkit/build-angular:server",
                "options": {
                    "outputPath": "dist/ClientApp/server",
                    "main": "server.ts",
                    "tsConfig": "tsconfig.server.json"
                },
                "configurations": {
                    "production": {
                        "outputHashing": "media",
                        "fileReplacements": [
                            {
                                "replace": "src/environments/environment.ts",
                                "with": "src/environments/environment.prod.ts"
                            }
                        ],
                        "sourceMap": false,
                        "optimization": true
                    }
                }
            },
            "serve-ssr": {
                "builder": "@nguniversal/builders:ssr-dev-server",
                "options": {
                    "browserTarget": "ClientApp:build",
                    "serverTarget": "ClientApp:server"
                },
                "configurations": {
                    "production": {
                        "browserTarget": "ClientApp:build:production",
                        "serverTarget": "ClientApp:server:production"
                    }
                }
            },
            "prerender": {
                "builder": "@nguniversal/builders:prerender",
                "options": {
                    "browserTarget": "ClientApp:build:production",
                    "serverTarget": "ClientApp:server:production",
                    "routes": ["/"]
                },
                "configurations": {
                    "production": {}
                }
            }
        }
    }
},
"defaultProject": "ClientApp"

}


Arkadiusz Idzikowski staff commented 4 years ago

We will take a closer look at this problem and let you know what we found. Please create a simple demo on which we will be able to reproduce this problem and send it to a.idzikowski@mdbootstrap.com. We need to check the ssr configuration and make sure that we use the same settings while debugging.


yuenkai free commented 4 years ago

I emailed simple demo to you. Please help to check


atrain0789 priority commented 4 years ago

Hi. Is there any update on this? I am having the same issue after upgrading to: "Error ReferenceError: KeyboardEvent is not defined"


Arkadiusz Idzikowski staff answered 4 years ago


Thank you for the demo. It looks like there is some bug with Event/KeyboardEvent type annotations in Angular HostListener. We need to change the implementation on our end in order to fix that, this problem should be fixed in next MDB Angular release (withing two weeks).

We tried some common workarounds for this kind of problems, however it looks like they doesn't work in this case. If we find any solution for this problem that you can add to your app before our fix release, we will post it here.


postoliuk pro commented 4 years ago

Hello, two weeks ago you promised fix bug with Event/KeyboardEvent. Do you have any updates?


Arkadiusz Idzikowski staff commented 4 years ago

We added some fixes for that in v9.0.1. Please let us know if you still experience any problems after the upgrade.



Please insert min. 20 characters.

FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Resolved

Specification of the issue

  • ForumUser: Free
  • Premium support: No
  • Technology: MDB Angular
  • MDB Version: 9.0.0
  • Device: PC
  • Browser: Google Chrome
  • OS: Windows 10
  • Provided sample code: No
  • Provided link: No
Tags