Typescript cannot find name window or document
Solution 1
It seems that the problem is caused by targeting ES2016
.
Are you targeting that for a reason? If you target es6
the error will probably go away.
Another option is to specify the libraries for the compiler to use:
tsc -t ES2016 --lib "ES2016","DOM" ./your_file.ts
Which should also make the error go away.
I'm not sure why the libs aren't used by default, in the docs for compiler options it states for the --lib
option:
Note: If --lib is not specified a default library is injected. The default library injected is:
► For --target ES5: DOM,ES5,ScriptHost
► For --target ES6: DOM,ES6,DOM.Iterable,ScriptHost
But it doesn't state what are the default libraries when targeting ES2016
.
It might be a bug, try to open an issue, if you do please share the link here.
Solution 2
use
"lib": ["dom"]
in tsconfig.json
e.g.
{
"compilerOptions": {
"lib": ["es5", "es6", "dom"],
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",
"jsx": "react"
},
"include": ["./src/**/*"]
}
Steven Bayer
Updated on March 06, 2020Comments
-
Steven Bayer about 4 years
For either case:
document.getElementById('body'); // or window.document.getElementById('body');
I get
error TS2304: Cannot find name 'window'.
Am I missing something in
tsconfig.json
for a definition file I should install?I get the message when running
tsc
and invscode
tsconfig.json:
{ "compilerOptions": { "allowJs": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "jsx": "react", "module": "commonjs", "moduleResolution": "node", "noEmitOnError": true, "noImplicitAny": false, "sourceMap": true, "suppressImplicitAnyIndexErrors": true, "target": "ES2016", "typeRoots": [ "node_modules/@types/", "typings/index.d.ts" ] }, "exclude": [ "node_modules", "**/*-aot.ts" ] }
My Answer: For use with
tsconfig.json
I targetes5
and uselib: ["es2015", "dom"]
-
Steven Bayer over 7 yearsTargeting ES6 did the trick! I originally targeted ES2016 because I plan to put the resulting javascript through webpack so I didn't think it would make much difference.
-
mightyiam over 7 yearsIt makes sense to me that
--lib
s would have to be explicitly declared instead of some arbitrary defaults. -
S. G. about 7 yearsI'm getting the same issue at code time in VSCode with an Angular 2 project built with Angular CLI, and switching to "es6" in both/either tsconfig.json and tsconfig.app.json didn't resolve it; neither did adding "dom" to tsconfig.json (it's already in tsconfig.app.json). There are no compile-time or runtime errors, but the red squiggly is annoying while coding.
-
Nitzan Tomer about 7 years@S.Greylyn If there are no compile errors then it's an issue with your VSCode. I've never used it.
-
Andrew Mairose almost 7 years@S.Greylyn use
"lib": ["dom"]
intsconfig.json
. That worked for me. -
Dominic about 6 yearsBaffled what target actually does, is it just for typedefs? And lib... is also for typedefs?
-
Nitzan Tomer about 6 years@DominicTobias
target
specifies to which version of js the output of the compilation. For example, if you want your code to run on older browsers, you don't wantes6
feature (and above). Thelib
option has default values based on thetarget
. You can override the defaults if you're sure that the environment at runtime does include the features. -
Ernesto over 5 yearsFWIW this is what solved it for me. Should be the accepted answer, because in some situations (starter kits or tools that already take care of running
tsc
for you, you're not so easily able to change the CLI args passed to it, but you almost surely controltsconfig.json
) -
horstwilhelm over 4 yearsAlso of note: If you already have
lib
in yourtsconfig.json
, it might help to remove it again.lib
will be autofilled with what makes sense for yourtarget
but if you have already definedlib
, it will be left as-is, meaning you might miss some libs. -
Adharsh M about 3 yearsi have added this to my lib but its not still working. "lib": [ "es5", "es6", "dom", "dom.iterable", "esnext" ],
-
Colin Sullivan about 3 yearsSame @AdharshM...I wonder is there a recent issue?
-
Vadorequest about 3 yearsI'm also encountering an issue, my code was working fine and then it wasn't, but I hadn't changed anything TS-related and was already using
dom
. I'll try use an older version of TS, I updated yesterday (but didn't notice this issue since) -
Vadorequest about 3 years@AdharshM This is likely due to github.com/lquixada/cross-fetch/issues/104, at least that was the case for me.