main.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'document')

600 views Asked by At

I Migrated my angular application to angular 14 and update all the packages and dependencies, I am building my code and is is building successfully and also while running ng serve it is not showing any error. But on browser is it showing error like this enter image description here

**Uncaught TypeError: Cannot read properties of undefined (reading 'document')
    at main.js:1:2247633
    at dt.exports.21 (main.js:1:2411389)
    at d (main.js:1:2195929)
    at main.js:1:2195963**

here is my package.json

 {
  "name": "lupita-portal",
  "version": "1.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "10": "^0.0.1",
    "-": "^0.0.1",
    "@angular/animations": "^14.2.8",
    "@angular/common": "^14.2.8",
    "@angular/compiler": "^14.2.8",
    "@angular/core": "^14.2.8",
    "@angular/forms": "^14.2.8",
    "@angular/localize": "^14.2.8",
    "@angular/platform-browser": "^14.2.8",
    "@angular/platform-browser-dynamic": "^14.2.8",
    "@angular/router": "^14.2.8",
    "@microsoft/signalr": "^6.0.10",
    "@ngx-i18nsupport/tooling": "^1.0.1",
    "@ngx-translate/i18n-polyfill": "^1.0.0",
    "@progress/kendo-angular-buttons": "^8.0.0",
    "@progress/kendo-angular-common": "^3.0.0",
    "@progress/kendo-angular-dateinputs": "^7.1.1",
    "@progress/kendo-angular-dialog": "^7.0.0",
    "@progress/kendo-angular-dropdowns": "^7.0.0",
    "@progress/kendo-angular-excel-export": "^5.0.0",
    "@progress/kendo-angular-grid": "^7.4.2",
    "@progress/kendo-angular-inputs": "^9.0.5",
    "@progress/kendo-angular-intl": "^4.0.0",
    "@progress/kendo-angular-l10n": "^4.0.0",
    "@progress/kendo-angular-label": "^4.0.0",
    "@progress/kendo-angular-layout": "^7.0.0",
    "@progress/kendo-angular-menu": "^4.1.1",
    "@progress/kendo-angular-pdf-export": "^4.0.0",
    "@progress/kendo-angular-popup": "^5.0.0",
    "@progress/kendo-angular-progressbar": "^3.0.0",
    "@progress/kendo-angular-sortable": "^2.1.0",
    "@progress/kendo-angular-treeview": "^7.0.0",
    "@progress/kendo-data-query": "^1.0.0",
    "@progress/kendo-drawing": "^1.6.0",
    "@progress/kendo-licensing": "^1.0.2",
    "@progress/kendo-theme-default": "^5.0.0",
    "acorn": "^7.1.0",
    "angular-oauth2-oidc": "^13.0.1",
    "classlist.js": "^1.1.20150312",
    "core-js": "^3.26.0",
    "crypto-js": "^3.1.9-1",
    "events": "^3.0.0",
    "file-saver": "^2.0.5",
    "flexboxgrid": "^6.3.1",
    "jquery": "^3.4.1",
    "ngx-bootstrap": "^4.3.0",
    "node-sass": "^7.0.3",
    "openseadragon": "^3.1.0",
    "plotly.js": "^2.18.1",
    "plotly.js-cartesian-dist": "^1.50.1",
    "raw-loader": "^4.0.2",
    "rxjs": "^7.8.1",
    "rxjs-compat": "^6.6.7",
    "ta-json": "^2.5.0",
    "ts-node": "^10.9.1",
    "tslib": "1.9.0",
    "typescript": "^4.8.4",
    "zone.js": "^0.11.8"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.11",
    "@angular/cli": "^14.2.8",
    "@angular/compiler-cli": "^14.2.8",
    "@angular/language-service": "^14.2.8",
    "@ngx-i18nsupport/ngx-i18nsupport": "^1.0.1",
    "@types/crypto-js": "^3.1.43",
    "@types/jasmine": "^4.3.0",
    "@types/jasminewd2": "2.0.3",
    "@types/node": "^18.11.9",
    "codelyzer": "^4.5.0",
    "jasmine-core": "2.99.1",
    "jasmine-spec-reporter": "4.2.1",
    "karma": "^6.4.2",
    "karma-chrome-launcher": "2.2.0",
    "karma-coverage-istanbul-reporter": "2.0.1",
    "karma-jasmine": "1.1.2",
    "karma-jasmine-html-reporter": "0.2.2",
    "protractor": "^3.3.0",
    "tslint": "5.11.0"
  }
}
1

There are 1 answers

0
VonC On

You have a similar error with angular/angular issue 50138: yes, it is for a different property, with a different Angular version, but it illustrates how you would debug your issue.

First, make sure your angular.json snippet has source maps enabled:

{
  ...
  "projects": {
    "your-project": {
      ...
      "architect": {
        "build": {
          ...
          "options": {
            ...
            "sourceMap": true
            ...
          }
        }
      }
    }
  }
}

Then, since the error stack trace you provided points to main.js:1:2247633, look into the source map to find exactly where this error is originating from in your source code.
You might come upon codes like packages/core/src/render3/interfaces/document.ts, which illustrates how a document can be undefined.

export function getDocument(): Document {
  if (DOCUMENT !== undefined) {
    return DOCUMENT;
  } else if (typeof document !== 'undefined') {
    return document;
  }
  // No "document" can be found. This should only happen if we are running ivy outside Angular and
  // the current platform is not a browser. Since this is not a supported scenario at the moment
  // this should not happen in Angular apps.
  // Once we support running ivy outside of Angular we will need to publish `setDocument()` as a
  // public API. Meanwhile we just return `undefined` and let the application fail.
  return undefined!;
}

See if your own code points toward a similar code.