Cannot find name 'console'. What could be the reason for this?
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
?
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
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
}
}
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"
]
}
just add ES6 and DOM in your tsconfig.json file
"lib": ["ES6", "DOM"]
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.
I had the same problem in node terminal. Adding node
to the types
field of tsconfig.json
solved my issue
It seems you are using typescript
so here are the steps you need to do.
- Install global
tsc
using
npm I typescript --global
call
tsc --init
if you don't have atsconfig.json
already in your folderIf you have a
tsconfig.json
in thelib
consider includingDOM
like this
"lib": ["DOM" ...],
Confirm that you don't import console
from anything. like:
import { console } from 'console'; // Confirm you haven't a statement like this.
ReferenceURL : https://stackoverflow.com/questions/42105984/cannot-find-name-console-what-could-be-the-reason-for-this
'programing' 카테고리의 다른 글
동일한 테이블의 다른 여러 행과 관련된 테이블 업데이트 시도 (0) | 2023.09.27 |
---|---|
Excel 매크로를 호출하고 매크로가 완료될 때까지 기다리는 VBA Outlook (0) | 2023.09.27 |
What APIs are used to draw over other apps (like Facebook's Chat Heads)? (0) | 2023.09.27 |
Select2 또는 Choosed에 대해 적절히 테스트된 대안이 있습니까? (0) | 2023.09.17 |
우커머스에서 주문을 병합하는 방법 (0) | 2023.09.17 |