Cannot find name 'document'. Do you need to change your target library? Try changing the `lib` compiler option to include 'dom'.ts(2584)
Asked Answered
C

1

10

I want to convert a html string to a html element in deno with typescript. I found this function for it:

function createElementFromHTML(htmlString: string) {
  var div = document.createElement('div');
  div.innerHTML = htmlString.trim();

  return div.firstChild; 
}

When I run it with this command:

deno run --allow-net .\scripts\API\fetchUtils.ts

It tells me this:

Cannot find name 'document'. Do you need to change your target library? Try changing the lib compiler option to include 'dom'.ts(2584)

After a bit of searching I found out that I need to add this to my tsconfig:

{
  "compilerOptions": {
    "allowJs": true,
    "esModuleInterop": true,
    "lib": ["esnext", "DOM"],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "pretty": true,
    "resolveJsonModule": true,
    "target": "esnext"
  },
  "include": [
    "./**/*.ts"
  ]
}

I still get the same error, restarts etc. didn't help.

I don't know if it makes a difference but this is how my project is structured from the standpoint of where I start running the deno file from the console:

scripts
  -API
    -fetchUtils.ts
tsconfig.json
Cleek answered 5/12, 2020 at 11:51 Comment(1)
Does including "DOM" in "lib" (tsconfig.json) work? Isn't it "dom" ?Kcal
T
3

See vscode_deno #318

deno.config might not be set as './tsconfig.json' (or path of it)

Just follow the issue mentioned if you are using vscode. If not, check your configuration

Topless answered 9/5, 2021 at 7:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.