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

Javascriptnode.jsTypescript

Javascript Problem Overview


The following snippet shows a typescript error at 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 it cannot find the object console?

Javascript Solutions


Solution 1 - Javascript

You will have to install the @types/node to get the node typings, You can achieve that by executing the below command,

npm install @types/node --save-dev

Hope this helps!

Solution 2 - Javascript

Add "dom" in 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
    }
}

Solution 3 - Javascript

You can run npm install @types/node -D, and then you need to add types:[ 'node'] into your tsconfig.json also.

package.json

"devDependencies": {
    "@types/node": "^15.0.3"
}

tsconfig.json

{
  "compilerOptions": {
    "composite": true,
    "outDir": "./dist",
    "rootDir": ".",
    "declaration": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "target": "es6",
    "types": [
      "node"
    ],
    "lib": [
      "es6"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Solution 4 - Javascript

just add ES6 and DOM in your tsconfig.json file

"lib": ["ES6", "DOM"]

Solution 5 - Javascript

you can also use the same values as in @tBlabs answer from command line, and you don't need to install anything beside typescript:

tsc test.ts --lib esnext,dom

you separate values with comma and you don't need esnext for console.log to work.

Solution 6 - Javascript

I had the same problem in node terminal. Adding node to the types field of tsconfig.json solved my issue

Solution 7 - Javascript

Confirm that you don't import console from anything. like:

import { console } from 'console'; // Confirm you haven't a statement like this.

Solution 8 - Javascript

There's a simpler, but hacky way to get console.log work: instead of console.log(message) write eval('console').log(message).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhelloJSONView Question on Stackoverflow
Solution 1 - JavascriptDavid RView Answer on Stackoverflow
Solution 2 - JavascripttBlabsView Answer on Stackoverflow
Solution 3 - JavascriptphostannView Answer on Stackoverflow
Solution 4 - JavascriptReza HeydariView Answer on Stackoverflow
Solution 5 - JavascriptjcubicView Answer on Stackoverflow
Solution 6 - JavascriptMahdi SadeghiView Answer on Stackoverflow
Solution 7 - JavascriptMohammed OsmanView Answer on Stackoverflow
Solution 8 - JavascriptYevhen PavliukView Answer on Stackoverflow