일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- swiftUI
- ios
- tshaped
- 코드트리
- 프론트엔드
- 코드트리챌린지
- velog
- 코딩테스트
- AppleDeveloperAcademy
- globalcommunity
- SWIFT
- react
- UIKit
- Xcode
- react-query
- 자바스크립트
- 프로그래머스
- JavaScript
- Front-end
- error
- frontend
- git
- Apple Developer Academy
- 회고
- TypeScript
- 알고리즘
- 코딩테스트실력진단
- NextJs
- iOSDeveloper
- 프로젝트
- Today
- Total
Moon Work
[Typescript] Cannot find name 'console' 에러 본문
linked list 알고리즘을 typescript로 변경하는 중 다음과 같이 console을 찾을 수 없다는 에러를 찾았다.
error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.
53 console.log(linkedList.find(1))
해결하는 방법은 아래 라인을 통해 @types/node를 설치해주면된다.
npm install @types/node --save-dev
또는 아래와 같이 tsc --init으로 tsconfig.json을 생성하고 lib에 dom을 추가해주는 방법도 있다.
"lib": [ "es6", "dom" ],
tsc를 통해 ts파일을 컴파일한 후에 js파일로 변경한다면 가능하지만 ts-node를 사용해서는 바로 가능하지 않았다. 추가적으로 검색해서 알아낸 부분은 @types/node가 Node.js 타입을 추가해준다. 라는 내용이었지만 잘 이해가 가지 않았다. 그러던중 아래의 설명을 읽고 nodejs 환경에서 필요한 type definition들을 사용할 수 있게 해주는 것이 @types/node에 있고 그렇기 때문에 추가해주어야 console과 같은 브라우저,node 환경에서 정의된 것을 사용할 수 있다는 것을 알게되었다.
This package is used to load in all type definitions when using typescript in node. When you add other packages, you also have to add their typings if they do not include them by default.
As an example, imagine you want to use lodash in your typescript application, you will need to install the type definitions (@types/lodash) too.
You don't need to worry about all this when you are not using typescript.
참고한 글
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....
stackoverflow.com
https://defineall.tistory.com/704
[Node.js] Node.js에서 Typescript 사용하기 / ts-node, @types/node
Node.js란? [Node.js] Node.js란? / 사용법 Node.js란? 기존의 브라우져에서만 사용되던 javascript를 브라우져밖에서도 사용할 수 있도록 만들어주는 프로그램 Node.js를 쓰는 곳 알림이나 실시간 대화같이 데
defineall.tistory.com
https://stackoverflow.com/questions/33535879/how-to-run-typescript-files-from-command-line
How to run TypeScript files from command line?
I'm having a surprisingly hard time finding an answer to this. With plain Node.JS, you can run any js file with node path/to/file.js, with CoffeeScript it's coffee hello.coffee and ES6 has babel-node
stackoverflow.com
'에러처리' 카테고리의 다른 글
[CORS] Cors를 처리해도 Cors 에러가 나는 경우(with Credentials) (0) | 2022.12.28 |
---|---|
에러처리: [eslint] Failed to load plugin 'jsx-a11y' declared in 'package.json (0) | 2022.12.03 |
[Typescript] React props key error (0) | 2022.08.10 |
[React + Typescript] IntrinsicAttributes 에러 (0) | 2022.08.09 |
[Typescript] type assertion error (0) | 2022.07.31 |