Typescript: Node projects with --lib es6: cannot find name 'console'

Created on 7 Jul 2016  ·  10Comments  ·  Source: microsoft/TypeScript

TypeScript Version: nightly (2.0.0-dev.20160706)

Code

// Before compiling: npm install @types/node
// Compile options: { "lib": ["es6"], "types": ["node"] }
process.cwd();          // OK
console.log('testing'); // ERROR: Cannot find name 'console'

Expected behavior:

Compiles without errors

Actual behavior:

console is not defined

More Info:

node.d.ts does not contain a definition for the global console instance or for the Console class. References to console are resolved against the definitions from lib.dom.d.ts. But now with the --lib option, Node projects can opt out of the ambient DOM typings. That brings up the problem that console is not defined.

It's not as simple as just copying the ambient console and Console definitions from lib.dom.d.ts into node.d.ts, because in browsers the Console constructor is globally available but in Node it is not. 'Hiding' Console under the NodeJS namepace in node.d.ts wont work either because then projects that _don't_ use the --lib option will get conflicting DOM and Node definitions and will see the error TS2403: Subsequent variable declarations must have the same type. Variable 'console' must be of type 'Console', but here has type 'Console'.

I'm testing out changes to node.d.ts that will work both with and without --lib, and will submit a PR to DefinitelyTyped if I can get it working.

@types

Most helpful comment

tsconfig , lib property add "dom"

All 10 comments

Looks like a definition bug in nose.d.ts

How to resolve this problem?

@frogcjn if you get your node typings using npm install @types/node, you'll have the updated node.d.ts that includes the Console definitions.

thanks @yortus

This seems to have caused this bug:
https://github.com/Microsoft/TypeScript/issues/13204

tsconfig , lib property add "dom"

thanks @gatspy

thanks @gatspy ,it works!

@gatspy thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  ·  3Comments

DanielRosenwasser picture DanielRosenwasser  ·  3Comments

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

fwanicka picture fwanicka  ·  3Comments

blendsdk picture blendsdk  ·  3Comments