Cannot find the name "console". What could be the reason for this?

The following snippet shows the typescript error in LINE 4 :

import {Message} from './class/message'; function sendPayload(payload : Object) : any{ let message = new Message(payload); console.log(message); // LINE 4 } 

The error says:

 [ts] Cannot find name 'console'. 

What could be the reason for this? Why can't he find the console object?

+6
source share
3 answers

You will need to install @types/node to get typical node characters, you can achieve this by running the command below,

 npm install @types/node 

Hope this helps!

+14
source

Add "dom" to your lib section in compilerOptions in tsconfig.json.

Example:

 { "compilerOptions": { "rootDir": "src", "outDir": "bin", "module": "commonjs", "noImplicitAny": false, "removeComments": true, "preserveConstEnums": true, "sourceMap": true, "target": "es5", "lib": [ "es6", "dom" <------- Add this "dom" here ], "types": [ "reflect-metadata" ], "moduleResolution": "node", "experimentalDecorators": true, "emitDecoratorMetadata": true } } 
+10
source

There is a simpler but hacker way to get console.log working: instead of console.log(message) write eval('console').log(message) .

+1
source

Source: https://habr.com/ru/post/1014790/


All Articles