0

I'm trying to access window.location.href and document.getElementByID in my > typescript. But getting an error as its undefined.

(node:3024) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: document is not defined

Even tried adding in my tsconfig.json file :

"lib": [
  "es6",
  "dom"
],
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "allowJs": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": [
        "node_modules/*",
        "src/types/*"
      ]
    }
  },
  "include": [
    "src/**/*"
  ]
}
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
Nirmal
  • 13
  • 9
  • Yes, include `"lib": ["dom", "es2017"]` in your `tsconfig.json` under `compilerOptions `. – Aaron Beall May 16 '19 at 13:28
  • Still not able to access, as its showing error: (node:3024) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: document is not defined – Nirmal May 17 '19 at 10:27
  • @AaronBeall: Could you please give any alternate solution – Nirmal May 17 '19 at 11:22
  • Ha, you're using Node. That's pertinent information. DOM does not exist in NodeJS. – Aaron Beall May 17 '19 at 13:16
  • @AaronBeall So no solutions for accessing DOM or window object in node. Is that what you are saying – Nirmal May 17 '19 at 13:18
  • Basically. DOM and window object doesn't exist in Node's runtime. That's a browser API. You can use something like jsdom or phantomjs in Node that emulate the DOM... but I have no idea what you're actually trying to do so can't recommend anything. – Aaron Beall May 17 '19 at 13:20
  • Im trying to access the dom element and checking window.location.host in my application for various purposes.But not using jquery, simply to avoid the bundle size. So that's why ended up with document object access for checking the input values in typescript – Nirmal May 17 '19 at 13:30
  • Need more context or this is a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). The error you posted comes from Node. You can't use DOM from Node. – Aaron Beall May 17 '19 at 16:24

0 Answers0