Typescript 에서 console을 인식하지 못하는 문제

남양주개발자

·

2022. 1. 18. 08:47

728x90
반응형

Typescript 에서 console을 인식하지 못하는 문제

로컬에서 타입스크립트 테스트를 위해 설정을 하는 와중에 console을 찾을 수 없다는 에러를 발견 해당 에러는 기본적인 @types/node가 설치되지 않아서 발생한 에러

[nodemon] starting `ts-node index.ts`
/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:750
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:1:1 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

1 console.log("hello!");
  ~~~~~~~

    at createTSError (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:750:12)
    at reportTSError (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:754:19)
    at getOutput (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:941:36)
    at Object.compile (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1243:30)
    at Module.m._compile (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1370:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1374:12)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
  diagnosticText: "\u001b[96mindex.ts\u001b[0m:\u001b[93m1\u001b[0m:\u001b[93m1\u001b[0m - \u001b[91merror\u001b[0m\u001b[90m TS2584: \u001b[0mCannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.\n" +
    '\n' +
    '\u001b[7m1\u001b[0m console.log("hello!");\n' +
    '\u001b[7m \u001b[0m \u001b[91m~~~~~~~\u001b[0m\n',
  diagnosticCodes: [ 2584 ]
}
[nodemon] app crashed - waiting for file changes before starting...[nodemon] starting `ts-node index.ts`
/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:750
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:1:1 - error TS2584: Cannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.

1 console.log("hello!");
  ~~~~~~~

    at createTSError (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:750:12)
    at reportTSError (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:754:19)
    at getOutput (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:941:36)
    at Object.compile (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1243:30)
    at Module.m._compile (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1370:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/ruden/Desktop/portfolio/2022/learn-types/node_modules/ts-node/src/index.ts:1374:12)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12) {
  diagnosticText: "\u001b[96mindex.ts\u001b[0m:\u001b[93m1\u001b[0m:\u001b[93m1\u001b[0m - \u001b[91merror\u001b[0m\u001b[90m TS2584: \u001b[0mCannot find name 'console'. Do you need to change your target library? Try changing the 'lib' compiler option to include 'dom'.\n" +
    '\n' +
    '\u001b[7m1\u001b[0m console.log("hello!");\n' +
    '\u001b[7m \u001b[0m \u001b[91m~~~~~~~\u001b[0m\n',
  diagnosticCodes: [ 2584 ]
}
[nodemon] app crashed - waiting for file changes before starting...

@types/node를 설치 후 로컬에서 테스트하면 정상적으로 잘 동작하는 것을 확인할 수 있다.

yarn add --dev @types/node
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.ts
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node index.ts`
hello!
[nodemon] clean exit - waiting for changes before restart
728x90
반응형
그리드형

이 포스팅은 쿠팡파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

💖 저자에게 암호화폐로 후원하기 💖

아이콘을 클릭하면 지갑 주소가자동으로 복사됩니다